try
{
//打开结果集
hr=m_recordset->Open(query,source,adOpenDynamic,adLockOptimistic,adCmdText);
ValEof=m_recordset->get_adoEOF(&ValEof);
while(true)
{
                       if(ValEof)break;
                        .
                        .
m_recordset->MoveNext();
m_recordset->get_adoEOF(&ValEof);
}
//关闭结果集
if(field!=NULL) field->Release();
m_recordset->Close();
}
catch(_com_error &e)
{
AfxMessageBox(e.ErrorMessage());
return false;
}上面是一段查询相关记录集的代码,如果查询的记录集不为空,也就是m_recordset内有记录时,程序运行正常,如果没有查到记录(比如要查询的表中没有记录为空表时)即m_recordset为空时,程序运行就异常。原因从上面代码中可以看出:
m_recordset->MoveNext();
m_recordset记录集为空还进行MoveNext当然出错,if(ValEof)break;并没有起到记录集为空的判断作用,所以,这里我必需在if(ValEof)break之前加个判断语句,处理查询到的记录集为空的情况,这个判断怎么写呢?

解决方案 »

  1.   

    http://www.pconline.com.cn/pcedu/empolder/gj/vc/0507/653859.html
    参考别人是怎么弄的
      

  2.   


    if (pRecordset->BOF){没查到结果}if (pRecordset->RecordCount == 0) {没查到结果}
      

  3.   

    if(FAILED(pRecordset->MoveNext()))
    {}
      

  4.   

    RecordCount有时候获取不到记录数,这个办法不行!
      

  5.   

    忘了是pRecordset->BOF还是pRecordset->EOF了,一个表示记录集的开头一个是结尾,所以就是用这个判断了,不需要pRecordset->RecordCount
      

  6.   


    我程序里用了if(ValEof)break;作判断啊,当查询到的记录集无记录时,该判断也没用!
      

  7.   

    http://blog.csdn.net/zyq5945/archive/2010/04/29/5541280.aspx
    ValEof=m_recordset->get_adoEOF(&ValEof);
    while(true)
    //-->
    _variant_t var;
    CString strName,strAge;
    // 清空列表框
    m_AccessList.ResetContent();
    strName=strAge="";
    // 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
    // 因为它有时会经常出现一些想不到的错误。jingzhou xu
    try
    {
    if(!m_pRecordset->BOF)
    m_pRecordset->MoveFirst();
    else
    {
    AfxMessageBox("表内数据为空");
    return;
    }
    // 读入库中各字段并加入列表框中
    while(!m_pRecordset->adoEOF)
    {
    var = m_pRecordset->GetCollect("Name");
    if(var.vt != VT_NULL)
    strName = (LPCSTR)_bstr_t(var);
    var = m_pRecordset->GetCollect("Age");
    if(var.vt != VT_NULL)
    strAge = (LPCSTR)_bstr_t(var);
    m_AccessList.AddString( strName + " --> "+strAge );
    m_pRecordset->MoveNext();
    }
    // 默认列表指向第一项,同时移动记录指针并显示
    m_AccessList.SetCurSel(0);
    OnSelchangeListaccess();
    }
    catch(_com_error& e)
    {
    dump_com_error(e);
    }
      

  8.   

    先用sql查询记录条数,然会0,就不要Move遍历了
      

  9.   

    if(!m_pRecordset->BOF)
                m_pRecordset->MoveFirst();
            else
            {
                AfxMessageBox("表内数据为空");
                return;
            }
    用这个足够了~