char szFilterTXT[]= "Access Files(*.accdb)|*.accdb|Access Files(*.mdb)|*.mdb|| ";
CFileDialog fileopenbox(TRUE, "TXT" ,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFilterTXT);
fileopenbox.m_ofn.lpstrTitle= "浏览 ";
if(fileopenbox.DoModal()!=IDOK)
return;
CString strFilename=fileopenbox.GetPathName();
//连接选中数据库
try
{
//判断对象是否已经连接。
if(m_pConnection->State)
m_pConnection->Close();
if(m_pRecordset->State)
m_pRecordset->Close();
if((strFilename.Right(4)==".mdb"))
{
//设置连接字符串
strFilename="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+strFilename;
}
else if(strFilename.Right(6)==".accdb")
{
strFilename="Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+strFilename;
}
//使用Open方法连接数据库
m_pConnection->Open((_bstr_t)strFilename,"","",adModeUnknown);
AfxMessageBox("数据库连接成功!");
}
catch(_com_error e)
{
AfxMessageBox("连接数据失败,请检查数据库路径是否正确!");
}
也可以catch里面把错误内容打出来看看;
e.Description()显示的内容是空。
if (FAILED(hr))
{
_com_error e(hr);
AfxMessageBox(e.ErrorMessage());
return;
}
估计是ADO没有装好,或者没有用AfxOleInit初始化。
try
{
//你的ADO代码
}
catch (_com_error& e)
{
CString strMsg;
strMsg.Format(_T("错误描述:%s\n错误消息%s"),
(LPCTSTR)e.Description(),
(LPCTSTR)e.ErrorMessage());
AfxMessageBox(strMsg);
}