_RecordsetPtr 的open方法执行查询语句后 _RecordsetPtr得到的是什么?

tendollor 2009-05-28 11:24:21

int ADO::getcount(CString tablename)
{
_RecordsetPtr pRst(__uuidof(Recordset));
CString strSQL;
strSQL.Format("select * from %s",tablename); //如果是select count(*) from %s pRst得到的又是什么?
pRst->Open(_variant_t(strSQL),_variant_t((IDispatch*)m_pConnection),adOpenDynamic,adLockOptimistic,adCmdText);
return (pRst->RecordCount);


}


pRst不是指向一个类似数据库查询结果的表的结构吗?
我的表里面有数据 为什么得到的pRst->RecordCount就是-1呢?
...全文
583 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
tendollor 2009-05-31
  • 打赏
  • 举报
回复
我的错

我跟进去调试看了 错误不出在这里了

错误出在这了

strItem.Format(_T("%s"),COleDateTime::GetCurrentTime().Format(_T("Created: %I:%M:%S %p, %m/%d/%Y")));


之前这里都是一切正常的

我再去别的区找答案吧 这里是数据库编程区 呵呵~
谢谢大家帮我 平均散分了!
tendollor 2009-05-31
  • 打赏
  • 举报
回复


int ADO::getcount(CString tablename)
{
_RecordsetPtr pRst(__uuidof(Recordset));
CString strSQL;
strSQL.Format("select * from %s",tablename);
pRst->CursorLocation = adUseClient;
// pRst->Open(_variant_t(strSQL),_variant_t((IDispatch*)m_pConnection),adOpenDynamic,adLockOptimistic,adCmdText);
pRst->Open((LPCTSTR)strSQL,_variant_t((IDispatch*)m_pConnection),adOpenStatic,adLockOptimistic,adCmdText);

/* int i=1;
while(!pRst->adoEOF)
{
i++;
pRst->MoveNext();
}
return i;
*/

return (pRst->RecordCount);


}



代码被我改成这样。。。运行时还是弹出错误提示!

shakaqrj 2009-05-29
  • 打赏
  • 举报
回复
Use the RecordCount property to find out how many records are in a Recordset object. The property returns -1 when ADO cannot determine the number of records or if the provider or cursor type does not support RecordCount. Reading the RecordCount property on a closed Recordset causes an error.

If the Recordset object supports approximate positioning or bookmarks—that is, Supports (adApproxPosition) or Supports (adBookmark), respectively, return True—this value will be the exact number of records in the Recordset, regardless of whether it has been fully populated. If the Recordset object does not support approximate positioning, this property may be a significant drain on resources because all records will have to be retrieved and counted to return an accurate RecordCount value.


先pRst->CursorLocation = adUseClient;
Lin 2009-05-29
  • 打赏
  • 举报
回复
俺认为:
要想RecordCount有效,要改变你Open游标的方式

pRst->Open(_variant_t(strSQL),_variant_t((IDispatch*)m_pConnection),adOpenDynamic,adLockOptimistic,adCmdText);
变成:
pRst->Open((LPCTSTR)strSQL,_variant_t((IDispatch*)m_pConnection),adOpenStatic,adLockOptimistic,adCmdText);

俺建议你试试。


或者你搜索MSDN关于ADO Cursor的说明,俺记得上面说得很清楚。
redleaf515 2009-05-29
  • 打赏
  • 举报
回复
[Quote=引用楼主 tendollor 的帖子:]
C/C++ code
int ADO::getcount(CString tablename)
{
_RecordsetPtr pRst(__uuidof(Recordset));
CString strSQL;
strSQL.Format("select * from %s",tablename); //如果是select count(*) from %s pRst得到的又是什么?
pRst->Open(_variant_t(strSQL),_variant_t((IDispatch*)m_pConnection),adOpenDynamic,adLockOptimistic,adCmdText);
return (pRst->RecordCount);


}



pRs…
[/Quote]
这里的Open()函数首先打开数据库,然后执行那条SQL命令.返回tablename表中的所有信息.
另外,使用Command对象和记录集对象都可以来对数据库进行操作.
redleaf515 2009-05-29
  • 打赏
  • 举报
回复
[Quote=引用楼主 tendollor 的帖子:]
C/C++ code
int ADO::getcount(CString tablename)
{
_RecordsetPtr pRst(__uuidof(Recordset));
CString strSQL;
strSQL.Format("select * from %s",tablename); //如果是select count(*) from %s pRst得到的又是什么?
pRst->Open(_variant_t(strSQL),_variant_t((IDispatch*)m_pConnection),adOpenDynamic,adLockOptimistic,adCmdText);
return (pRst->RecordCount);


}



pRs…
[/Quote]
这里的Open()函数首先打开数据库,然后执行那条SQL命令.返回tablename表中的所有信息.
另外,使用Command对象和记录集对象都可以来对数据库进行操作.
dengxuxing 2009-05-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xsc2001 的回复:]
_RecordsetPtr执行open以后打开的应该是记录集,如果返回-1则说明你的Open打开时失败了。
[/Quote]

说得对
lvbajiao 2009-05-28
  • 打赏
  • 举报
回复
很正常的结果。
要返回正确的记录数,或使用while,movenext等
或者指定_COnnectionPtr某属性为aduseclient.
xsc2001 2009-05-28
  • 打赏
  • 举报
回复
_RecordsetPtr执行open以后打开的应该是记录集,如果返回-1则说明你的Open打开时失败了。
biweilun 2009-05-28
  • 打赏
  • 举报
回复
连接建立之后,修改属性无效
biweilun 2009-05-28
  • 打赏
  • 举报
回复
可以
tendollor 2009-05-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 gameslq 的回复:]
int ADO::getcount(CString tablename)
{
_RecordsetPtr pRst(__uuidof(Recordset));
CString strSQL;
strSQL.Format("select * from %s",tablename); //如果是select count(*) from %s pRst得到的又是什么?
/*加上下面这句就可以了*/
pRst->CursorLocation = adUseClient;
pRst->Open(_variant_t(strSQL),_variant_t((IDispatch*)m_pConnection),adOpenDynamic,adLockOptimistic,adCmdTe…
[/Quote]


在这里加上这句程序直接出错

是不是连接建立之后再改变属性就不起作用

我在其他的文章中看到的 而且改的是_COnnectionPtr连接的属性 这个是记录集的这个属性能行吗?
gameslq 2009-05-28
  • 打赏
  • 举报
回复
int ADO::getcount(CString tablename)
{
_RecordsetPtr pRst(__uuidof(Recordset));
CString strSQL;
strSQL.Format("select * from %s",tablename); //如果是select count(*) from %s pRst得到的又是什么?
/*加上下面这句就可以了*/
pRst->CursorLocation = adUseClient;
pRst->Open(_variant_t(strSQL),_variant_t((IDispatch*)m_pConnection),adOpenDynamic,adLockOptimistic,adCmdText);
return (pRst->RecordCount);


}
biweilun 2009-05-28
  • 打赏
  • 举报
回复
http://www.programfan.com/blog/article.asp?id=16229
好好学习这个你就都知道了
tendollor 2009-05-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lvbajiao 的回复:]
很正常的结果。
要返回正确的记录数,或使用while,movenext等
或者指定_COnnectionPtr某属性为aduseclient.
[/Quote]

能说的详细点吗
tendollor 2009-05-28
  • 打赏
  • 举报
回复
open 的时候没失败 我调试过来都正确的

而且我换成

_RecordsetPtr pRst(__uuidof(Recordset));
CString strSQL;
strSQL.Format("select * from %s",tablename);
pRst->Open(_variant_t(strSQL),_variant_t((IDispatch*)m_pConnection),adOpenDynamic,adLockOptimistic,adCmdText);
int i=1;
while(!pRst->adoEOF)
{
i++;
pRst->MoveNext();
}
return i;


这就得到正确的结果了
但是如果记录数是0的时候得到的记录数成了1

既然有pRst->RecordCount 为什么不能用

4,018

社区成员

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

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