我的程序如下:
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
m_pConnection->Open("Provider=SQLOLEDB.1;Persist Security Info=True;Initial Catalog=sbj_cw;Data Source=sbj;UID=sa;PWD=***","","",adModeUnknown);
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return FALSE;
}
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open("SELECT * FROM users",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
但是我用m_pRecordset->GetRecordCount()得到的值为-1;不知为什么?
如何可得到数据集m_pRecordset中的记录条数
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
m_pConnection->Open("Provider=SQLOLEDB.1;Persist Security Info=True;Initial Catalog=sbj_cw;Data Source=sbj;UID=sa;PWD=***","","",adModeUnknown);
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return FALSE;
}
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open("SELECT * FROM users",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
但是我用m_pRecordset->GetRecordCount()得到的值为-1;不知为什么?
如何可得到数据集m_pRecordset中的记录条数
for(int i = 0; !m_Set->EndOfFile; i++)
{
Holder = m_Set->GetCollect("CHANNEL_ID");
if (Holder.vt != VT_NULL)
//Do what you want here
}
m_pRecordset->Open("SELECT * FROM users",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);int total;
total=0;
while(!m_pRecordset->adoEOF)
{
total++;
m_pRecordset->MoveNext();
}
请注意以下这段话:
GetRecordCount is not guaranteed to return the correct number of rows for a table-type recordset even if you call this function after calling MoveLast(). The value of GetRecordCount from a table-type recordset reflects the approximate number of records in the table. To get an accurate record count, open a dynaset- or snapshot-type recordset and then do a MoveLast. Table-type recordsets will be accurate immediately after the database has been compacted or repaired。
GetRecordCount只有在open a dynaset- or snapshot-type recordset and then do a MoveLast时才能正常返回个数。
建议:采用上面zlxcjy(晚霞) 的方法