关于 ado 的 recordsetPtr 使用

whhvc 2011-09-18 10:56:40
recordsetPtr的open方法 什么时候会一次返回所有记录到内存?
经常出现异常,错误消息为 对象已经使用 是怎么回事啊?
...全文
79 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
whhvc 2011-09-19
  • 打赏
  • 举报
回复
_RecordsetPtr p(__uuidof(Recordset));

p->Open( stringSql,pSrcConn.GetInterfacePtr(),adOpenStatic,adLockReadOnly,adCmdText);
DWORD nCount = p->RecordCount;

RELEASE_RECORDSET( p); // if openstatus close
p.Release();
p.~_com_ptr_t();
WWWWA 2011-09-19
  • 打赏
  • 举报
回复
示例:
_variant_t vUsername,vID,vname; //变量声明
_RecordsetPtr m_pRecordset; //记录集
CString strid;
_ConnectionPtr connection;
m_pRecordset.CreateInstance(__uuidof( Recordset )); //创建实例
m_pRecordset->Open("SELECT * FROM users",connection.GetInterfacePtr

(),adOpenStatic,adLockOptimistic,adCmdText);//执行SQL语句,得到记录集, connection必须已和数

m_pRecordset->Close();

m_pConnection->Close();

m_pRecordset=NULL;

m_pConnection=NULL;
whhvc 2011-09-19
  • 打赏
  • 举报
回复
#define RELEASE_XXX(p)\
try{\
if(p!=NULL){ if( p->GetState()==adStateOpen ) p->Close(); }\
}catch(...)\
{\
;\
}


如上 close的



另数据不是一次返回,只能通过你的程序循环读取,是在内存读吗? 需要数据库吗?我是静态获取的,数据库改变不影响我的
whhvc 2011-09-19
  • 打赏
  • 举报
回复
_RecordsetPtr 使用
_RecordsetPtr xxx(__uuidof(Recordset));

xxx->Open(strSQL.c_str(),pDestConn.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);


错误号 = 80040e05
错误信息:IDispatch error #3077
错误源:Microsoft OLE DB Provider for SQL Server
错误描述:对象被打开。
ACMAIN_CHM 2011-09-19
  • 打赏
  • 举报
回复
[Quote]经常出现异常,错误消息为 对象已经使用 是怎么回事啊?[/Quote]这个recordsetPtr你有没有CLOSE?

if (pRstEmployee)
if (pRstEmployee->State == adStateOpen)
pRstEmployee->Close();
if (pConnection)
if (pConnection->State == adStateOpen)
pConnection->Close();
ACMAIN_CHM 2011-09-19
  • 打赏
  • 举报
回复
[Quote]recordsetPtr的open方法 什么时候会一次返回所有记录到内存?[/Quote]不会一次返回所有记录,只能通过你的程序循环读取。


pRs->Open("SELECT * FROM Employee ORDER BY lname",
"dsn=pubs;uid=sa;pwd=;",
adOpenStatic, adLockOptimistic, adCmdText);

TESTHR(picRs->BindToRecordset(&rs));

while (!pRs->EndOfFile)
{
// Process data in the CCustomRs C++ instance variables.
printf("Name = %s %s\n",
(rs.m_ul_fnameStatus == adFldOK ? rs.m_ch_fname: "<Error>"),
(rs.m_ul_lnameStatus == adFldOK ? rs.m_ch_lname: "<Error>"));

// Move to the next row of the Recordset.
// Fields in the new row will automatically be
// placed in the CCustomRs C++ instance variables.

pRs->MoveNext();
}
WWWWA 2011-09-19
  • 打赏
  • 举报
回复
你的代码是什么,使用前定义、关闭没有

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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