// 方法1 CString strSQL; //1 strSQL.Format("SELECT SID FROM Equipment group by SID");//得到总的行数 CoInitialize(NULL); _ConnectionPtr m_pConnection; _RecordsetPtr m_pRecordset; try{ m_pConnection.CreateInstance("ADODB.Connection"); m_pConnection->CursorLocation=adUseClient; //1 游标类型 m_pConnection->ConnectionString="Provider=SQLOLEDB.1;Password=; / Persist Security Info=True;User ID=sa;Initial Catalog=PTMSE"; m_pConnection->Open("","","",adConnectUnspecified);// } catch(...) { MessageBox("数据库连接失败!"); } m_pRecordset=m_pConnection->Execute((_bstr_t)strSQL,NULL,adCmdText);//select * from History_Record authors // CString str=(LPCSTR)_bstr_t(m_pRecordset->Fields->GetCount());//得到查询字段长度 //1。 // long count=(long)(m_pRecordset->GetRecordCount());//();// long count=(long)(m_pRecordset->RecordCount); CString str; str.Format("%d",count); MessageBox(str); m_pRecordset->Close(); m_pConnection->Close(); m_pRecordset.Release(); m_pConnection.Release(); CoUninitialize(); //方法2: CString strSQL; //2{{{ strSQL.Format("SELECT count(distinct(SID)) as Total FROM Equipment");// CoInitialize(NULL); _ConnectionPtr m_pConnection; _RecordsetPtr m_pRecordset; try{ m_pConnection.CreateInstance("ADODB.Connection"); m_pConnection->CursorLocation=adUseClient; //1 游标类型 m_pConnection->ConnectionString="Provider=SQLOLEDB.1;Password=; / Persist Security Info=True;User ID=sa;Initial Catalog=PTMSE"; m_pConnection->Open("","","",adConnectUnspecified); } catch(...) { MessageBox("数据库连接失败!"); } m_pRecordset=m_pConnection->Execute((_bstr_t)strSQL,NULL,adCmdText);//select * from History_Record authors // 2。 // CString str=(LPCSTR)(_bstr_t)m_pRecordset->GetCollect("Total");//直接由程序得到结果,总的纪录数 // MessageBox(str); m_pRecordset->Close(); m_pConnection->Close(); m_pRecordset.Release(); m_pConnection.Release(); CoUninitialize();