连接数据库的代码如下,可是抛出异常,连接不成功,请问如何解决?
BOOL CADOTest1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
HRESULT hr;
_ConnectionPtr m_pConnection; //指向Connection对象的指针
try
{
//创建Connection对象
hr=m_pConnection.CreateInstance("ADODB.Connection");
if(SUCCEEDED(hr))
{
//连接数据源
hr=m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;"
"Data Source=RealEstate.mdb",
"","",adModeUnknown);
//Provider是针对Access2000环境的
}
}
catch(_com_error & e) //捕捉异常
{
CString errormessage;
errormessage.Format(_T("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage()));
AfxMessageBox(errormessage);
return FALSE;
}