如何重复使用_ConnectionPtr 和_RecordSetPtr访问数据库?

luouII 2002-06-26 10:40:24
我的程序里需要不停的重复访问数据库,请问如何使用_ConnectionPtr 和 _RecordSetPtr来访问数据库?
...全文
154 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
luouII 2002-06-26
  • 打赏
  • 举报
回复
to ZHENG017(风中王子)
请问 Create _RecordsetPtr的效率怎么样?会不会消耗很多的系统资源?
ZHENG017 2002-06-26
  • 打赏
  • 举报
回复
最好了,每次使用RecordsetPtr都重新create,而_ConnectionPtr打开后,到析构才close吧.
_ConnectionPtr是智能指针,哈哈哈,comptr,可以不考虑内存泄漏
luouII 2002-06-26
  • 打赏
  • 举报
回复
to storein(满天星):
谢谢,我想做一个class把_RecordsetPtr,_ConnectionPtr包在里面,初始化的时候建立连接,使用中是否可以直接调用_RecordsetPtr->Close(),然后在Destructor里面做_ConnectionPtr->Close()?主要是关心会不会有内存或者系统资源泄漏的问题。
storein 2002-06-26
  • 打赏
  • 举报
回复
你就在程序的APP中连接数据库,
以后你想使用连接的时候
AfxGetApp()
就可以得到你的连接(定义成PUBLIC)
退出程序才关闭

至于纪录集,在那里用就在那里定义,用了释放就完了

luouII 2002-06-26
  • 打赏
  • 举报
回复
to jiayp004(spark):需要做m_m_pConnectionection->Close ();么?
那么下一次是否还要做m_m_pConnectionection->open?
我访问的是SQL Server的数据库
jiayp004 2002-06-26
  • 打赏
  • 举报
回复
在重新使用前置空
m_pRecordset->Close (); //-3
m_m_pConnectionection->Close ();
m_pRecordset = NULL;
m_m_pConnectionection = NULL;
kingzai 2002-06-26
  • 打赏
  • 举报
回复
/=====================Open dbf database file
#include "stdafx.h"
#import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "EndOfFile")
int main(int argc, char* argv[])
{
printf("Use ADO to open c:\\tmp\\images.dbf database file!\n");
CoInitialize(NULL);
try
{
_ConnectionPtr pConn("ADODB.Connection");
_RecordsetPtr pRst("ADODB.Recordset");
pConn->Open("Driver={Microsoft dBASE Driver (*.dbf)};DBQ=C:\\tmp\\; DriverID=533;"
,"","",adConnectUnspecified);
pRst->Open("images", _variant_t((IDispatch *) pConn, true),
adOpenStatic, adLockReadOnly, adCmdTable);
FieldsPtr fds=pRst->GetFields();
printf("printf field name of all the table\n");
for(int i=0;i<fds->GetCount();i++)
{
FieldPtr fd=fds->GetItem(_variant_t(short(i)));
printf("%s ",(LPCTSTR)fd->GetName());
}
printf("\n");
pRst->Close();
pConn->Close();
}
catch (_com_error &e)
{
printf("Description = '%s'\n", (char*) e.Description());
}
::CoUninitialize();
return 0;
}
kingzai 2002-06-26
  • 打赏
  • 举报
回复
exmaple:
/=====================Open dbf database file
#include "stdafx.h"
#import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "EndOfFile")
int main(int argc, char* argv[])
{
printf("Use ADO to open c:\\tmp\\images.dbf database file!\n");
CoInitialize(NULL);
try
{
_ConnectionPtr pConn("ADODB.Connection");
_RecordsetPtr pRst("ADODB.Recordset");
pConn->Open("Driver={Microsoft dBASE Driver (*.dbf)};DBQ=C:\\tmp\\; DriverID=533;"
,"","",adConnectUnspecified);
pRst->Open("images", _variant_t((IDispatch *) pConn, true),
adOpenStatic, adLockReadOnly, adCmdTable);
FieldsPtr fds=pRst->GetFields();
printf("printf field name of all the table\n");
for(int i=0;i<fds->GetCount();i++)
{
FieldPtr fd=fds->GetItem(_variant_t(short(i)));
printf("%s ",(LPCTSTR)fd->GetName());
}
printf("\n");
pRst->Close();
pConn->Close();
}
catch (_com_error &e)
{
printf("Description = '%s'\n", (char*) e.Description());
}
::CoUninitialize();
return 0;
}
kingzai 2002-06-26
  • 打赏
  • 举报
回复
exmaple:
/=====================Open dbf database file
#include "stdafx.h"
#import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "EndOfFile")
int main(int argc, char* argv[])
{
printf("Use ADO to open c:\\tmp\\images.dbf database file!\n");
CoInitialize(NULL);
try
{
_ConnectionPtr pConn("ADODB.Connection");
_RecordsetPtr pRst("ADODB.Recordset");
pConn->Open("Driver={Microsoft dBASE Driver (*.dbf)};DBQ=C:\\tmp\\; DriverID=533;"
,"","",adConnectUnspecified);
pRst->Open("images", _variant_t((IDispatch *) pConn, true),
adOpenStatic, adLockReadOnly, adCmdTable);
FieldsPtr fds=pRst->GetFields();
printf("printf field name of all the table\n");
for(int i=0;i<fds->GetCount();i++)
{
FieldPtr fd=fds->GetItem(_variant_t(short(i)));
printf("%s ",(LPCTSTR)fd->GetName());
}
printf("\n");
pRst->Close();
pConn->Close();
}
catch (_com_error &e)
{
printf("Description = '%s'\n", (char*) e.Description());
}
::CoUninitialize();
return 0;
}
我花钱买的,结果没用上,太亏了 本系统采用ADO来访问SQL数据库,这里充分应用了C++类封装的功能,根据本系统应用的范围,将访问数据库的功能函数封装在一个类CMyDatabase。 正如前所述,ADO是访问数据库的一个方法,它提供了不同的接口。ADO库包含三个基本接口:_ConnectionPtr接口、_CommandPtr接口和_RecordsetPtr接口。 _ConnectionPtr接口返回一个记录集或一个空指针。通常使用它来创建一个数据连接或执行一条不返回任何结果的SQL语句,如一个存储过程。使用_ConnectionPtr接口返回一个记录集不是一个好的使用方法。对于要返回记录的操作通常用_RecordserPtr来实现。而用_ConnectionPtr操作时要想得到记录条数得遍历所有记录,而用_RecordserPtr时不需要。 _CommandPtr接口返回一个记录集。它提供了一种简单的方法来执行返回记录集的存储过程和SQL语句。在使用_CommandPtr接口时,你可以利用全局_ConnectionPtr接口,也可以在_CommandPtr接口里直接使用连接串。如果你只执行一次或几次数据访问操作,后者是比较好的选择。但如果你要频繁访问数据库,并要返回很多记录集,那么,你应该使用全局_ConnectionPtr接口创建一个数据连接,然后使用_CommandPtr接口执行存储过程和SQL语句。 _RecordsetPtr是一个记录集对象。与以上两种对象相比,它对记录集提供了更多的控制功能,如记录锁定,游标控制等。同_CommandPtr接口一样,它不一定要使用一个已经创建的数据连接,可以用一个连接串代替连接指针赋给_RecordsetPtrconnection成员变量,让它自己创建数据连接。如果你要使用多个记录集,最好的方法是同Command对象一样使用已经创建了数据连接的全局_ConnectionPtr接口 ,然后使用_RecordsetPtr执行存储过程和SQL语句。  根据这些,我们将数据库的各种操作封装到CMyDatabase类里。几个主要函数说明如下:

4,018

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧