1.
首先在stdafx.h导入
#import "C:\program files\common files\System\ado\msado15.dll" no_namespace \
rename("EOF","adoEOF")
2.在app类中添加
public:
_RecordsetPtr m_pRs;
_ConnectionPtr m_pConn;
bool ADOExecute(_RecordsetPtr &ADOSet, _variant_t &strSQL);
在APP类中的初始化函数
if( FAILED(::CoInitialize(NULL)) )
{
AfxMessageBox(_T("ADO Init failed"));
return false;
}
try
{
m_pConn.CreateInstance(__uuidof(Connection));
//m_pConn->Open("Driver=Mysql ODBC 5.1 Driver;Server=localhost;DataBase=student;Port=3306","root","root",adModeUnknown);
}
// Catch Exceptions
catch(_com_error e)
{
CString err;
err.Format(_T("%s"), (char*)e.Description());
AfxMessageBox(err);
}
catch(...)
{
AfxMessageBox(_T("Unknown Error..."));
}
// Init ADO RecordSet
m_pRs.CreateInstance(__uuidof(Recordset));
添加函数
bool CduiApp::ADOExecute(_RecordsetPtr &ADOSet, _variant_t &strSQL)
{
if ( ADOSet->State == adStateOpen)
ADOSet->Close();
try
{
ADOSet->CursorType = adOpenStatic;
ADOSet->CursorLocation = adUseClient;
ADOSet->Open(strSQL, m_pConn.GetInterfacePtr(), adOpenStatic, adLockOptimistic, adCmdUnknown);
return true;
}
catch(_com_error &e)
{
CString err;
err.Format(_T("ADO Error: %s"),(char*)e.Description());
AfxMessageBox(err);
return false;
}
}
然后测试查询:
theApp.ADOExecute(theApp.m_pRs,_variant_t(“select * from user”));
int iCount=theApp.m_pRs->GetRecordCount();
if (iCount>0)
{
while (!theApp.m_pRs->adoEOF)
{
m_list.InsertItem(0,(LPCTSTR)(_bstr_t)theApp.m_pRs->GetCollect("id"));
}
}
ok