标签:style class blog code http ext
最近在用ADO访问Access数据库的时候出现了一个奇怪的错误,觉得有必要记录下来,和大家分享一下。
win7 x86系统;
VS2012编译器;
Office2010;
Access2000~Access2003连接串建立数据库连接。
BOOL CAccessDatabase::Query(const CString strSQLString, UINT nFieldNumber, vector<vector<_variant_t> >& vecvecVariant) { _variant_t Variant; vector<_variant_t> vecVariant; EnterCriticalSection(&m_cs); _RecordsetPtr pRecordset = NULL; HRESULT hr = S_FALSE; try { hr = pRecordset.CreateInstance(_T("ADODB.Recordset")); if (SUCCEEDED(hr)) { hr = pRecordset->Open(strSQLString.AllocSysString(), m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText); if (SUCCEEDED(hr)) { if (!pRecordset->adoEOF) { hr = pRecordset->MoveFirst(); if (SUCCEEDED(hr)) { while (!(pRecordset->adoEOF)) { vecVariant.clear(); for (UINT i = 0; i < nFieldNumber; ++i) { ZeroMemory(&Variant, sizeof(Variant)); Variant = pRecordset->GetCollect(_variant_t((long)i)); vecVariant.push_back(Variant); } vecvecVariant.push_back(vecVariant); hr = pRecordset->MoveNext(); } } else { m_pLogFile->WriteLog(GetLastError(), _T("移动记录集指针到首部发生错误")); if (pRecordset->GetState() != adStateClosed) { hr = pRecordset->Close(); if (FAILED(hr)) { m_pLogFile->WriteLog(GetLastError(), _T("关闭记录集失败")); } else { m_pLogFile->WriteLog(GetLastError(), _T("记录集已关闭")); } } if (pRecordset != NULL) { pRecordset.Release(); pRecordset = NULL; } LeaveCriticalSection(&m_cs); return FALSE; } } else { m_pLogFile->WriteLog(GetLastError(), _T("查询的记录集为空")); } } else { m_pLogFile->WriteLog(GetLastError(), _T("打开记录集失败")); if (pRecordset != NULL) { pRecordset.Release(); pRecordset = NULL; } LeaveCriticalSection(&m_cs); return FALSE; } } else { m_pLogFile->WriteLog(GetLastError(), _T("初始化记录集失败")); pRecordset = NULL; LeaveCriticalSection(&m_cs); return FALSE; } } catch (_com_error e) { m_pLogFile->WriteLog(e.ErrorMessage()); AfxMessageBox(e.ErrorMessage()); LeaveCriticalSection(&m_cs); return FALSE; } if (pRecordset->GetState() != adStateClosed) { try { hr = pRecordset->Close(); if (FAILED(hr)) { m_pLogFile->WriteLog(GetLastError(), _T("关闭记录集失败")); } else { m_pLogFile->WriteLog(GetLastError(), _T("记录集已关闭")); } } catch (_com_error err) { m_pLogFile->WriteLog(err.ErrorMessage()); AfxMessageBox(err.ErrorMessage()); } } if (pRecordset != NULL) { pRecordset.Release(); pRecordset = NULL; } LeaveCriticalSection(&m_cs); return TRUE; }
if (!Database.Query(_T("select ZoneName from Zone"), 1, vecvecZone)) { LogFile.WriteLog(GetLastError(), _T("从数据库中获取客户端名称失败")); AfxMessageBox(_T("从数据库中获取客户端名称失败")); return; }
ADO访问Access数据库错误解决心得随笔,布布扣,bubuko.com
标签:style class blog code http ext
原文地址:http://www.cnblogs.com/qiyueliuguang/p/3807744.html