CString strSql = _T("SELECT * FROM qwerasdf");//table是關鍵詞
_bstr_t bstrSql = strSql;
m_access.GetRecordSet(bstrSql);
FieldsPtr pFields = m_access.m_pRecordset->GetFields();
int nCount = pFields->GetCount();
FieldPtr pField;
CString strTitle, strValue;
_variant_t varValue;
////未知欄位名
//for (int i = 0; i < nCount; i++)
//{
// pField = pFields->GetItem(i);//_com_error
// strTitle.Format(_T("%s"), pField->Name);
// MessageBox(strTitle);
//}
//已知欄位名
if (!m_access.m_pRecordset->adoEOF)
{
int iValue;
while (!m_access.m_pRecordset->adoEOF)
{
varValue = m_access.m_pRecordset->GetCollect(_variant_t(0));//_com_error
varValue = m_access.m_pRecordset->GetCollect("ID");
iValue = varValue.intVal;
strValue.Format(_T("%d"), iValue);
MessageBox(strValue);
//varValue = m_access.m_pRecordset->GetCollect(_variant_t(1));//_com_error
varValue = m_access.m_pRecordset->GetCollect("value1");
strValue = varValue.bstrVal;
MessageBox(strValue);
//varValue = m_access.m_pRecordset->GetCollect(_variant_t(2));//_com_error
varValue = m_access.m_pRecordset->GetCollect("value2");
strValue = varValue.bstrVal;
MessageBox(strValue);
m_access.m_pRecordset->MoveNext();
}
}
如上述代碼
pField = pFields->GetItem(i);
varValue = m_access.m_pRecordset->GetCollect(_variant_t(0));//這是第0列的意思嗎
varValue = m_access.m_pRecordset->GetCollect(_variant_t(1));
varValue = m_access.m_pRecordset->GetCollect(_variant_t(2));
這兩句為什么都提示
0x75E446D2 處(位于xxx.exe 中)有未經處理的例外: Microsoft C++ 例外: _com_error,位于記憶體位置 0x00BEE87C 處。
我看很多例子都是這么寫的啊,我這里就不正常
屏蔽掉這2句可以正常查詢到資料庫中的資料,排除我資料庫連不上資料不對的情況
再有一個問題
m_access.m_pRecordset->GetRecordCount();//-1
這個回傳值為什么一直是-1,遍歷前和遍歷后都是-1
uj5u.com熱心網友回復:
GetCollect(_variant_t(n)) n是列索引try catch 捕捉例外
void PrintComError(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
// Print COM errors.
printf("Error\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR) bstrSource);
printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}
{
try
{
……
}
catch (_com_error &e)
{
PrintComError(e);
}
記錄數可以試試 SQL的 SELECT COUNT(*) FROM tablename
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/263963.html
標籤:數據庫
