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);
就能显示第二条了,谢谢!!!

解决方案 »

  1.   

    在第一种代码下,i的值没有被正确使用。
    m_ListCtrl.InsertItem(i, str);//在ListCtrl显示了一条记录。
                                  //且索引为i(此时为0)。
    如果要再显示一条记录,还是执行
    m_ListCtrl.InsertItem(i, str);//但要确保i的值与先前不一样。
    在第一种代码下,i自加后就没被使用了,你使用的是if结构。
    用个循环试试。
      

  2.   

    if(!m_pRecordset->adoEOF)
    改成
    while(!m_pRecordset->adoEOF)
      

  3.   

    http://community.csdn.net/Expert/topic/4097/4097931.xml?temp=.8695642#reply
      

  4.   

    汗,不好意思,怎么回复了上面的东西改成while不行啊,出错,而且用while时输入-> ,vc2003没有出现列表,就是因为这样我才用if的
      

  5.   

    把循环条件改成:
    for (int i = 0; i < m_pRecordset->GetRecordCount(); i++)
    也不行,奇怪,i失效了,再改成例如:
    for (int i = 0; i < 5; i++)就行
      

  6.   

    int i=0;try
    {
       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);
    就能显示第二条了,谢谢!!!
      

  7.   

    说一句闲话,GetRecordCount()不好使跟while/if没有关系,试一下m_pRecordset->RecordCount。
    为什么GetRecordCount()不管用我也不清楚,查过msdn还是不明所以然……
    等待高手指教中
      

  8.   

    adOpenDynamic,选项打开时..记录数是-1.
    改用adOpenStatic.这时才能正常读取记录数