4,011
社区成员
发帖
与我相关
我的任务
分享
_ConnectionPtr m_pConnection;
m_pConnection.CreateInstance(__uuidof(Connection));
try
{
CString connsStr(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="));
connsStr += m_dbName;
m_pConnection->Open((_bstr_t)connsStr,_T(""),_T(""),adModeUnknown);
}
catch(_com_error e)
{ //错误有一个线程,到一定的条件就调用此函数,在Access存贮20M内容左右,
运行56分钟后,会出现打开失败。此后就一直是打开失败
_bstr_t bstrDescription(e.Description());
CString strErro=(LPCSTR)e.Description();
CDebug::ShowDetailMessage(_T("open DB failed!"));
CDebug::ShowDetailMessage(strErro.GetBuffer());
strErro.ReleaseBuffer();
strErro = (LPCSTR)e.ErrorMessage();
CDebug::ShowDetailMessage(strErro.GetBuffer());
if(m_pConnection->State)
{
m_pConnection->Close();
m_pConnection.Release();
}
m_pConnection= NULL;
return FALSE;
}
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
CString sql(_T("SELECT * FROM "));
sql = sql + m_dbTable;
m_pRecordset->Open((_variant_t)sql, // 查询DemoTable表中所有字段
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error e)
{
_bstr_t bstrDescription(e.Description());
CString err = (LPCSTR)e.Description();
CDebug::ShowDetailMessage(err.GetBuffer());
return FALSE;
}
try
{
//插入数据部分省略
m_pRecordset->Update();
}
catch(_com_error e)
{
_bstr_t bstrDescription(e.Description());
CString err = (LPCSTR)e.Description();
CDebug::ShowDetailMessage(err.GetBuffer());
}
m_pRecordset->Close();
m_pRecordset.Release();
m_pRecordset = NULL;
//关闭数据库
if(m_pConnection->State)
{
m_pConnection->Close();
m_pConnection.Release();
}
m_pConnection= NULL;
return TRUE;