vc++6.0使用的oracle 9i 如何轉換成access2007?
使用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;
}