我是打算在VC中通过ADO连接access数据库,然后将数据库中表格的部分内容显示到列表中。程序编译和连接都通过已经通过了,但要实现上述功能时,运行出现了错误。
下面将主要的程序贴出来,希望大家帮忙看一下:
m_list1.DeleteAllItems();
m_list1.SetRedraw(FALSE); 
_ConnectionPtr m_pConnection;
AfxOleInit();
 
m_pConnection.CreateInstance(_uuidof(Connection));
try
{
         m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Adult.mdb", "", "",       adModeUnknown);//连接数据库
 
}
catch(_com_error e)//捕捉异常
{
AfxMessageBox("数据库链接失败,确认数据库是否在当前路径下!");//显示错误信息errormessage
} _RecordsetPtr m_pRecordset;

m_pRecordset.CreateInstance(_uuidof(Recordset));
try
{
m_pRecordset->Open("select * from AdultTable",_variant_t((IDispatch*)m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
} _variant_t var;
CString str,strAge,strWorkclass,strEducation,strMaritalstatus,strOccupation,strRace,strSex,strNativecountry;
int i=0;
try
{
if(!m_pRecordset->BOF)
m_pRecordset->MoveNext();
else
{
AfxMessageBox("表内数据为空!");
return ;
}
while(!m_pRecordset->EndOfFile)
{      
var=m_pRecordset->GetCollect("age");
if(var.vt!=VT_NULL)
strAge=(LPCSTR)_bstr_t(var);
var=m_pRecordset->GetCollect("workclass");
if(var.vt!=VT_NULL)
strWorkclass=(LPCSTR)_bstr_t(var);
var=m_pRecordset->GetCollect("education");
if(var.vt!=VT_NULL)
strEducation=(LPCSTR)_bstr_t(var);
var=m_pRecordset->GetCollect("marital-status");
if(var.vt!=VT_NULL)
strMaritalstatus=(LPCSTR)_bstr_t(var);
var=m_pRecordset->GetCollect("occupation");
if(var.vt!=VT_NULL)
strOccupation=(LPCSTR)_bstr_t(var);
var=m_pRecordset->GetCollect("race");
if(var.vt!=VT_NULL)
strRace=(LPCSTR)_bstr_t(var);
var=m_pRecordset->GetCollect("sex");
if(var.vt!=VT_NULL)
strSex=(LPCSTR)_bstr_t(var);
var=m_pRecordset->GetCollect("native-country");
if(var.vt!=VT_NULL)
strNativecountry=(LPCSTR)_bstr_t(var);

m_list1.SetItemText(i,0,strAge);
m_list1.SetItemText(i,1,strWorkclass);
m_list1.SetItemText(i,2,strEducation);
m_list1.SetItemText(i,3,strMaritalstatus);
m_list1.SetItemText(i,4,strOccupation);
m_list1.SetItemText(i,5,strRace);
m_list1.SetItemText(i,6,strSex);
m_list1.SetItemText(i,7,strNativecountry); i++;
m_pRecordset->MoveNext();
}
 

}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
m_list1.SetRedraw(TRUE);
return;
}
 
m_pRecordset->Close(); //关闭记录集 
m_pConnection->Close(); //关闭连接 m_list1.SetRedraw(TRUE); 运行后错误提示是:Runtime Eorror!
                Program:E:\...\Debug\***.exe
                This application has requested the Runtime to terminate it in an unusual way.Please contace ...
本人是这方面新手,网上查了很多资料,也没能看出个究竟来。大家帮帮忙啊,谢啦!