我对这个问题进行了修改,发现了这两个链接:
在第一个链接中,它描述了仅当更改默认选项时,Native Client 10才使用服务器端游标:
When these options are set to their defaults at the time an SQL statement is executed,the SQL Server Native Client ODBC driver does not use a server cursor to implement the result set; instead,it uses a default result set.
Link 2是一个博客,它是一个SQL Dev博客,它说:
It turned out that the developer did not explicitly ask for a server cursor. But when he did block fetches,as a side effect,the SQL Server ODBC driver asked for a server cursor…that is unexpected!
是的,当然这是出乎意料的……
How can I do block fetches over a Default Result Set (fire hose cursor) instead of a server cursor?
现在,解决方案的实施是这样的:
代替:
// crs is a CRecordSet
crs.Open(CRecordset::snapshot,L"SELECT something...");
做这个:
// crs is a CRecordSet
crs.Open(CRecordset::forwardOnly,L"SELECT something...");
这个简单的更改不会触发创建服务器端游标,并模仿MySQL驱动程序的行为.
缺点是现在您无法通过(Microsoft推荐)方式检索行计数:
while(crs.MoveNext()) nCount++;
无论如何,这是一个坏主意.此外,:: SQLGetRowCount()将不再一直有效.
我已经解决了这个问题(这应该适用于任何ANSI兼容的SQL源):
//strQuery is the random query passed to CountRows()
std::wstringstream ssQuery;
ssQuery << L"SELECT COUNT(*) AS Count FROM (" << strQuery << L") AS t";
CRecordset crs(&m_Database);
crs.Open(CRecordset::forwardOnly,ssQuery.str().c_str());
// Now retrieve the only "Count" field from the recordset.
我希望将来可以帮助别人.