Ado 实现C++对象的存取
2006-07-23 11:19:25 来源:WEB开发网二、用Ado接口打开数据库
BOOL CTestAdoDlg::OpenDb(CString filename)
(注意:在StdAfx.h中要加入:
{
HRESULT hr=S_OK;
hr=m_pCon.CreateInstance("ADODB.Connection");
if (hr!=S_OK)
{
return FALSE;
}
try
{
_bstr_t sCon;
sCon=_bstr_t(filename); //路径名
sCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+sCon;
hr=m_pCon->Open(sCon,"","",adModeUnknown);
if (hr!=S_OK)
{
return FALSE;
}
///////////////////////
hr=m_pSet.CreateInstance("ADODB.Recordset");
if (hr!=S_OK)
{
return FALSE;
}
m_pSet->CursorLocation=adUseClient;
hr=m_pSet->Open("SELECT * FROM object_table",_variant_t((IDispatch*)m_pCon,TRUE),
adOpenStatic,adLockOptimistic,adCmdText);
if (hr!=S_OK)
{
return FALSE;
}
return TRUE;
///////////////////////
}
catch(_com_error &e)
{
CString errorMessage;
errorMessage.Format("连接数据库失败!错误信息:%s",e.ErrorMessage());
return FALSE;
}
return FALSE;
} #import "C:\Program Files\Common Files\SYSTEM\ADO\msado15.dll" no_namespace rename("EOF","adoEOF")
来引入ado库,还有在 BOOL CTestAdoApp::InitInstance() 加入 AfxOleInit();///初始化COM库)
更多精彩
赞助商链接