_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呢?
...全文
642 16 打赏 收藏 转发到动态 举报
写回复
用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 为什么不能用
内容概要:本文围绕“基于超局部模型与自抗扰ESO观测器的无模型预测电流控制改进策略”展开研究,提出一种结合超局部模型(ULM)与扩张状态观测器(ESO)的无模型预测电流控制(MFPCC)改进方法,旨在提升永磁同步电机(PMSM)电流环的动态响应性能与抗干扰能力。该策略利用超局部模型对系统行为进行局部逼近,避免依赖精确数学模型,同时引入自抗扰控制中的ESO实时观测并补偿系统内外部扰动,有效抑制参数摄动、负载变化及模型不确定性带来的影响。研究通过Simulink搭建完整的控制系统仿真模型,对传统MFPCC与所提改进策略进行对比分析,验证了新方法在电流跟踪精度、响应速度和鲁棒性方面的优越性。; 适合人群:具备电机控制、现代控制理论及Simulink仿真基础的电气工程、自动化及相关专业的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于高性能电机驱动系统中电流环控制器的设计与优化;②为无模型控制与自抗扰控制的融合应用提供技术参考;③支撑相关课题的仿真验证、论文复现与创新方法研究。; 阅读建议:建议读者结合Simulink仿真模型深入理解控制结构与参数整定过程,重点关注ESO的观测性能与扰动补偿机制,并可通过改变负载条件、参数偏差等工况进行鲁棒性测试,进一步掌握该改进策略的核心优势与适用边界。
内容概要:本文围绕Scratch图形化编程平台,详细阐述了《人体感应灯光系统》这一贴近生活的AI科创作品的设计与教学应用。通过模拟真实智能家居中人体感应灯的工作原理,利用Scratch的侦测、逻辑判断、亮度特效调节等功能,实现了人物靠近自动亮灯、延时熄灭及环境亮度自适应等仿真功能。文章系统拆解了从场景搭建、核心逻辑设计、分层编程实现到调试优化的完整开发流程,并提供了基础版与进阶版可直接导入的源码,支持零基础快速上手与高阶创新拓展。同时构建了“基础—进阶—高阶”三层阶梯式教学体系,适配常规课堂、创客社团与赛事培优等多元教学场景,推动中小学AI教育的生活化、实践化与创新化发展。 适合人群:小学高年级至初中阶段学生,信息技术教师,创客教育从业者,以及参与青少年科创赛事的师生。 使用场景及目标:①作为中小学人工智能通识课程的教学案例,帮助学生理解智能感应与控制逻辑;②用于校内创客社团开展项目式学习;③支撑学生参加AI科创类赛事,完成高质量作品创作与答辩准备;④布置为课后综合实践作业,提升动手能力与科技素养。 阅读建议:建议结合提供的Scratch源码进行实践操作,在复现基础上尝试参数调优与功能扩展,如增加音效提示、多区域感应等,深化对编程逻辑与智能系统设计的理解。

4,017

社区成员

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

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