ADODB::_RecordsetPtr::GetRecordCount() failure

ADODB::_RecordsetPtr::GetRecordCount() failure

本文关键字:failure RecordsetPtr ADODB GetRecordCount      更新时间:2023-10-16

我正在尝试使用ADO在C++中执行一些查询。这是我的代码

string commandline = "SELECT * FROM My_Table";
ADODB::_RecordsetPtr pRS("ADODB.Recordset");
ADODB::_ConnectionPtr pConn("ADODB.Connection");
pRS->Open(commandline.c_str(), _variant_t((IDispatch *) pConn, true), ADODB::adOpenUnspecified,  ADODB::adLockUnspecified, ADODB::adCmdText);
cout<<pRS->GetRecordCount();

我在My_Table中有1000条记录,因此我希望看到输出1000。但是,输出是-1

我能知道我做错了什么吗?

非常感谢。

RecourdCount属性只有在底层提供程序或游标类型实际支持的情况下才有效,否则返回-1。

尝试使用静态光标(传递ADODB::adOpenStatic而不是ADODB::adOpenUnspecified

还请注意,即使提供程序支持,获取记录计数也可能会占用大量资源,因为提供程序必须先获取所有记录,然后才能知道受查询影响的记录数。

有关详细文档,请参阅此MSDN页面。