我是这样做的,写了个FreshList()函数,在数据库读取数据时用于刷新列表。然后就是在对话框初始化时调用这个函数,使得数据库中表的信息显示在列表控件上。但是程序运行后抛出异常(“刷新列表失败”),调试后发现应该是这个函数写的有问题,求大神指教,小弟就剩最后这点分了,不要嫌少啊!(已引入ADO库文件,初始化com库环境)
下面是程序和数据表结构
void CQueryDlg::FreshList()
{
try
{
m_pRecordset->Open("SELECT * FROM 产品流转信息表",(IDispatch*)m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);
int nItem;
m_InformationList.DeleteAllItems();
while(!m_pRecordset->adoEOF)
{
nItem=m_InformationList.GetItemCount();
CString str;
str=(LPCTSTR)_bstr_t(m_pRecordset->GetCollect("产品编码"));
m_InformationList.InsertItem(nItem,str);
str=(LPCTSTR)_bstr_t(m_pRecordset->GetCollect("产品名称"));
m_InformationList.SetItemText(nItem,1,str);
str=(LPCTSTR)_bstr_t(m_pRecordset->GetCollect("产品状态"));
m_InformationList.SetItemText(nItem,2,str);
str=(LPCTSTR)_bstr_t(m_pRecordset->GetCollect("交易单位"));
m_InformationList.SetItemText(nItem,3,str);
str=(LPCTSTR)_bstr_t(m_pRecordset->GetCollect("流转时间"));
m_InformationList.SetItemText(nItem,4,str);
str=(LPCTSTR)_bstr_t(m_pRecordset->GetCollect("运输车辆牌号"));
m_InformationList.SetItemText(nItem,5,str);
str=(LPCTSTR)_bstr_t(m_pRecordset->GetCollect("信息备注"));
m_InformationList.SetItemText(nItem,6,str);
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("刷新列表失败!");
}
}
数据表结构
列表控件