GetRecordCount()返回记录条数为啥总为-1

来源:互联网 发布:华小夏汉服淘宝 编辑:程序博客网 时间:2024/05/21 11:13

执行类似以下语句后,得到结果总是-1.

pRecordSet = pConnection->Execute(bstrQuery,NULL, adCmdText);pRecordSet->GetRecordCount();

原因在于默认游标是向前搜索的,所以在连接上数据库后设置游标即可。

try  {  pConnection->Open (strConnect,"","", adModeUnknown); //连接数据库  <strong>pConnection->CursorLocation = adUseClient;</strong>}  catch(_com_error e)  {  AfxMessageBox("连接数据库时出错\n");  exit(0);  } 

或者直接执行查询语句。

pRecordSet = pConnection->Execute("select count(*) as total from testViewData where year>=1990 and year<=2006 and latitude between 3200 and 4200 and longitude between 10800 and 12400", NULL, adCmdText);int tempCount = int(pRecordSet->GetCollect("total"));

第二种方法较好。

0 0