int i=0;try
{
m_pRecordset->MoveFirst(); if(!m_pRecordset->adoEOF)
{
_variant_t vField = m_pRecordset->GetCollect("StuName");
CString str = (LPCSTR)_bstr_t(vField);
if(str!="")
m_ListCtrl.InsertItem(i, str);
m_pRecordset->MoveNext();
i++;
}
}
catch (_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}只能显示第一条记录如果改成:
_variant_t vField = m_pRecordset->GetCollect("StuName");
CString str = (LPCSTR)_bstr_t(vField);
if(str!="")
m_ListCtrl.InsertItem(0, str);
m_pRecordset->MoveNext();_variant_t vField = m_pRecordset->GetCollect("StuName");
CString str = (LPCSTR)_bstr_t(vField);
if(str!="")
m_ListCtrl.InsertItem(1, str);
就能显示第二条了,谢谢!!!
{
m_pRecordset->MoveFirst(); if(!m_pRecordset->adoEOF)
{
_variant_t vField = m_pRecordset->GetCollect("StuName");
CString str = (LPCSTR)_bstr_t(vField);
if(str!="")
m_ListCtrl.InsertItem(i, str);
m_pRecordset->MoveNext();
i++;
}
}
catch (_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}只能显示第一条记录如果改成:
_variant_t vField = m_pRecordset->GetCollect("StuName");
CString str = (LPCSTR)_bstr_t(vField);
if(str!="")
m_ListCtrl.InsertItem(0, str);
m_pRecordset->MoveNext();_variant_t vField = m_pRecordset->GetCollect("StuName");
CString str = (LPCSTR)_bstr_t(vField);
if(str!="")
m_ListCtrl.InsertItem(1, str);
就能显示第二条了,谢谢!!!
m_ListCtrl.InsertItem(i, str);//在ListCtrl显示了一条记录。
//且索引为i(此时为0)。
如果要再显示一条记录,还是执行
m_ListCtrl.InsertItem(i, str);//但要确保i的值与先前不一样。
在第一种代码下,i自加后就没被使用了,你使用的是if结构。
用个循环试试。
改成
while(!m_pRecordset->adoEOF)
for (int i = 0; i < m_pRecordset->GetRecordCount(); i++)
也不行,奇怪,i失效了,再改成例如:
for (int i = 0; i < 5; i++)就行
{
m_pRecordset->MoveFirst(); //if(!m_pRecordset->adoEOF)
while(!m_pRecordset->adoEOF){
//_variant_t vField = m_pRecordset->GetCollect("StuName");
//CString str = (LPCSTR)_bstr_t(vField);
//if(str!="")
//m_ListCtrl.InsertItem(i, str);
if(m_pRecordset->GetCollect("StuName").vt!=VT_NULL){
CString strVal=m_pRecordset->GetCollect("StuName").bstrVal;
m_ListCtrl.InsertItem(i,strVal);
}
else{
m_ListCtrl.InsertItem(i,_T(""));
}
m_pRecordset->MoveNext();
i++;
}
m_pRecordset->Close();
}
catch (_com_error &e){
//MessageBox(e->ErrorMessage());
MessageBox(e.Description());
}只能显示第一条记录如果改成:
_variant_t vField = m_pRecordset->GetCollect("StuName");
CString str = (LPCSTR)_bstr_t(vField);
if(str!="")
m_ListCtrl.InsertItem(0, str);
m_pRecordset->MoveNext();_variant_t vField = m_pRecordset->GetCollect("StuName");
CString str = (LPCSTR)_bstr_t(vField);
if(str!="")
m_ListCtrl.InsertItem(1, str);
就能显示第二条了,谢谢!!!
为什么GetRecordCount()不管用我也不清楚,查过msdn还是不明所以然……
等待高手指教中
改用adOpenStatic.这时才能正常读取记录数