二、使用ADO连接数据库,因为要使用COM,需要初始化(CoInitialize).然后可以生成接口的对象操作,代码如下:
#import "C:Program Files/Common Files/System/ado/msado15.dll" rename("EOF", "adoEOF")    //生成C++类,改变EOF函数的名称
using namespace ADODB;
BOOL ADOConnect(CString strDBFile)
{
_ConnectionPtr pConnection;
if(pConnection.CreateInstance(__uuidof(Connection)) != S_OK)
{
return FALSE;
}
CString strConnect;
strConnect.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s"), strDBFile);
if(pConnection->Open(_bstr_t(strConnect), "", "", adModeUnknown) == S_OK)
{
//连接数据库成功
_RecordsetPtr pRecordset;
if(pRecordset.CreateInstance(__uuidof(Recordset)) != S_OK)
{
pConnection->Close();
return FALSE;
}
CString strSql;
strSql = _T("select * from info");
HRESULT hr = pRecordset->Open(_bstr_t(strSql), _variant_t((IDispatch*)pConnection, TRUE), adOpenUnspecified, adLockUnspecified, adCmdUnknown);
if(hr != S_OK)
{
pConnection->Close();
return FALSE;
}
_variant_t vt;
pRecordset->MoveFirst();
while(!pRecordset->adoEOF)
{
vt = pRecordset->Fields->GetItem("姓名")->Value;
pRecordset->MoveNext();
}
pConnection->Close();
return TRUE;
}
return FALSE;
}