ADO訪問Access資料庫錯誤解決

來源:互聯網
上載者:User

標籤:style   http   io   color   ar   os   sp   for   strong   

最近在用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;

}

錯誤問題

程式在Query裡面產生了異常。在調用pRecordset->Open(…)的時候產生了異常,異常錯誤資訊是"未指定的錯誤";

將調用的Sql語句放到Access資料庫中可以正確執行。雅思答案

解決辦法

將資料庫的表名"Zone"修改為"MyZone"等其它名稱即可。

總結

在編程訪問Access資料庫時表名不能為"Zone".

ADO訪問Access資料庫錯誤解決

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.