我在连接数据库的时候用下面的代码:
LPCTSTR m_strConnect1 = "select f9_0002 as ExchangeID From dbo.tb_object_0002";ADOConn    m_Ado;
m_Ado.GetRecordSet( m_strConnect1 );
int stockRecorsCount = m_Ado.GetRecordCount(m_strConnect1); //纪录集的行数代码在执行到int stockRecorsCount = m_Ado.GetRecordCount(m_strConnect1); //纪录集的行数
这条语句的时候就崩溃了,select f9_0002 as ExchangeID From dbo.tb_object_0002 在数据库中查询是有记录的,但是如果我把LPCTSTR m_strConnect1 = "select f9_0002 as ExchangeID From dbo.tb_object_0002";这条语句改成LPCTSTR m_strConnect1 = "select f1_1010 as TradeDay from dbo.tb_object_1010 order by f1_1010 asc";
就可以找到结果集,请问是什么原因呢?

解决方案 »

  1.   

    加异常捕获看是什么错误
    try
    {
    //你的ADO代码
    }
    catch (_com_error& e)
    {
    AfxMessageBox(e.Description());
    }
      

  2.   

    记录集或者数据库连接已经调用了close
      

  3.   

    我的代码是:
    LPCTSTR m_strConnect1 = "select f9_0002 as ExchangeID From dbo.tb_object_0002";
    CString TradeDay;
    if( m_resultConn )
    {
    try
    {
    m_Ado.GetRecordSet( m_strConnect1 );
    bool m_result = m_Ado.GetCollect("ExchangeID", TradeDay);
    int stockRecorsCount = m_Ado.GetRecordCount(m_strConnect1); //纪录集的行数
    }
    catch (_com_error& e)
    {
    AfxMessageBox(e.Description());
    }
    }下面是我的接口函数:
    _RecordsetPtr& ADOConn::GetRecordSet(LPCTSTR lpszSQL)
    {
    try
    {
    // Create the result set object
    m_pRecordset.CreateInstance(__uuidof(Recordset));// m_pRecordset->CursorType = adOpenStatic; 
    // m_pRecordset->CursorLocation = adUseClient; // Get the records in the table
    m_pRecordset->Open(_bstr_t(lpszSQL),m_pConnection.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
    }
    // Catch Exception
    catch(_com_error e)
    {
    // 显示错误信息
    // TRACE(e.Description());
    //  sprintf(error,"\t执行SELECT语句失败\r\nSELECT语句为:%s",lpszSQL);
    //  throw new CADOException(error);
    // throw new  CADOException("执行SELECT语句失败");
    }
    //return the result set
    return m_pRecordset;
    }//Get the value of a field
    BOOL ADOConn::GetCollect(LPCTSTR Name,CString &lpDest)
    {
     VARIANT  vt;
     try
     {
      vt = m_pRecordset->GetCollect(Name);
     /* _bstr_t bstr = (_bstr_t)vt;
      if(lpDest != "")
      {
       strcpy(lpDest,bstr);
      }*/
      if(vt.vt!=VT_NULL)
       lpDest=(LPCSTR)_bstr_t(vt);
      else
       lpDest="";  return TRUE;
     }
     catch (_com_error e)
     {
    //  TRACE(e.Description());
    //  sprintf(error,"获取字段:%s值失败",Name);
    //  throw new CADOException(error);
     }
     return FALSE;
    }int ADOConn::GetRecordCount(const char* str)
    {
    return m_pRecordset->GetRecordCount();
    }
    我没有调用close()啊
      

  4.   

    在调用GetRecordCount()函数之前,确保你的CRecordSet至少有过一次MoveLast()
    http://topic.csdn.net/t/20000420/13/7308.html