在初始化时提示:Open database error!(3) UnKnown error 0x800A0E81
单步调试在运行至:m_pCommand->ActiveConnection = pConnection;时发生异常.
CString strSqlConnText;
_ConnectionPtr pConnection;
_RecordsetPtr m_pRecordset;
_CommandPtr m_pCommand;
BOOL CPXESER::InitDataBase(CString strDBName)
{
TCHAR szFilePath[MAX_PATH + 1];
GetModuleFileName(NULL, szFilePath, MAX_PATH);
(_tcsrchr(szFilePath, _T('\\')))[1] = 0; strSqlConnText.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s%s;Mode=ReadWrite|Share Deny None;Persist Security Info=False"),szFilePath,strDBName);
if(!AfxOleInit())
{
AfxMessageBox("Initialize OLEDB Library error!");
return false;
}
pConnection.CreateInstance(__uuidof(Connection));
m_pCommand.CreateInstance(__uuidof(Command));
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
pConnection->CursorLocation = adUseClient;
pConnection->Open((_bstr_t)strSqlConnText,"","",adModeShareDenyNone);
}
catch(_com_error e)
{
CString strTemp="";
strTemp.Format("Open database error!(1)\r\nError Message:%s",e.ErrorMessage());
AfxMessageBox(strTemp);
return false;
}
catch (CException* e)
{
char errmsg[255]="";
e->GetErrorMessage(errmsg,255);
CString strTemp ;
strTemp.Format("Open database error!(2)\r\n%s",errmsg);
AfxMessageBox(strTemp);
return false;
}
try
{
m_pCommand->ActiveConnection = pConnection;
m_pCommand->CommandType = adCmdText;
}
catch(_com_error e)
{
CString strTemp="";
strTemp.Format("Open database error!(3)\r\nError Message:%s",e.ErrorMessage());
AfxMessageBox(strTemp);
return false;
}
catch (CException* e)
{
char errmsg[255]="";
e->GetErrorMessage(errmsg,255);
CString strTemp ;
strTemp.Format("Open database error!(4)\r\n%s",errmsg);
AfxMessageBox(strTemp);
return false;
}
return true;
}
单步调试在运行至:m_pCommand->ActiveConnection = pConnection;时发生异常.
CString strSqlConnText;
_ConnectionPtr pConnection;
_RecordsetPtr m_pRecordset;
_CommandPtr m_pCommand;
BOOL CPXESER::InitDataBase(CString strDBName)
{
TCHAR szFilePath[MAX_PATH + 1];
GetModuleFileName(NULL, szFilePath, MAX_PATH);
(_tcsrchr(szFilePath, _T('\\')))[1] = 0; strSqlConnText.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s%s;Mode=ReadWrite|Share Deny None;Persist Security Info=False"),szFilePath,strDBName);
if(!AfxOleInit())
{
AfxMessageBox("Initialize OLEDB Library error!");
return false;
}
pConnection.CreateInstance(__uuidof(Connection));
m_pCommand.CreateInstance(__uuidof(Command));
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
pConnection->CursorLocation = adUseClient;
pConnection->Open((_bstr_t)strSqlConnText,"","",adModeShareDenyNone);
}
catch(_com_error e)
{
CString strTemp="";
strTemp.Format("Open database error!(1)\r\nError Message:%s",e.ErrorMessage());
AfxMessageBox(strTemp);
return false;
}
catch (CException* e)
{
char errmsg[255]="";
e->GetErrorMessage(errmsg,255);
CString strTemp ;
strTemp.Format("Open database error!(2)\r\n%s",errmsg);
AfxMessageBox(strTemp);
return false;
}
try
{
m_pCommand->ActiveConnection = pConnection;
m_pCommand->CommandType = adCmdText;
}
catch(_com_error e)
{
CString strTemp="";
strTemp.Format("Open database error!(3)\r\nError Message:%s",e.ErrorMessage());
AfxMessageBox(strTemp);
return false;
}
catch (CException* e)
{
char errmsg[255]="";
e->GetErrorMessage(errmsg,255);
CString strTemp ;
strTemp.Format("Open database error!(4)\r\n%s",errmsg);
AfxMessageBox(strTemp);
return false;
}
return true;
}
连接字符串是没错的.
我用SQL测试过了.
调试到这里时,m_pCommand为空?
pConnection.CreateInstance(__uuidof(Connection));
m_pCommand.CreateInstance(__uuidof(Command));
m_pRecordset.CreateInstance(__uuidof(Recordset));
在m_pCommand->ActiveConnection = pConnection;之前加入:Sleep(500);就可以了.
但具体不知道是为什么?