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

luouII 2002-06-26 10:40:24
我的程序里需要不停的重复访问数据库,请问如何使用_ConnectionPtr 和 _RecordSetPtr来访问数据库?
...全文
199 9 打赏 收藏 转发到动态 举报
写回复
用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;
}

4,017

社区成员

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

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