用如下方式在一个空表中查询时出错(我的目的是看表中是否存在指定数据),具体错误信息我没捕获到,只是查询时程序主界面停止(死机一样).不知如何改进才能正确.
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
CString strSQL;
strSQL.Format("SELECT DISTINCT %s FROM %s WHERE %s='%s'",field,tablename,field,str);
m_pRecordset->Open((_bstr_t)strSQL,theApp.m_pConn.GetInterfacePtr(),adOpenDynamic,adLockPessimistic,adCmdText);

解决方案 »

  1.   

    我将语句:_RecordsetPtr m_pRecordset; 
    m_pRecordset.CreateInstance(__uuidof(Recordset)); 
    CString strSQL; 
    strSQL.Format("SELECT DISTINCT %s FROM %s WHERE %s='%s'",field,tablename,field,str); 
    m_pRecordset->Open((_bstr_t)strSQL,theApp.m_pConn.GetInterfacePtr(),adOpenDynamic,adLockPessimistic,adCmdText);改为:_variant_t RecordsAffected;
    m_pRecordset =theApp.m_pConn->Execute(bstr_t(strSQL),&RecordsAffected,adCmdText);
    _variant_t vIndex = (long)0; 
    int row = (m_pRecordset->GetCollect(vIndex)).lVal;
    if(row<1)
    re=false;
    m_pRecordset->Close();捕获到错误:BOF 或EOF 中有一个是“真”,或者当前的记录已被删除,所需的操作要求一个当前的记录
    是不是我的ADO没打补丁的问题啊。
      

  2.   

    1、第一句,将adLockPessimistic改为adLockReadOnly试试,是否记录被锁导致被语句被阻塞。
    2、int row = (m_pRecordset->GetCollect(vIndex)).lVal;之前,先判断得到的记录集是否为空
    if(m_pRecordset->adoEOF && m_pRecordset->BOF) //记录集为空
       MessageBox("无记录");
    else
    {
    int row = (m_pRecordset->GetCollect(vIndex)).lVal; 
    if(row <1) 
    re=false;
    }
      

  3.   

    已解决,SELECT COUNT(*) AS MYROWS FROM TABLENAME,然后int rows=m_pRecordset->GetCollect{"MYROWS").intVt,再if(rows>))……,繁琐了些,但解决问题是关键。