//其中strSQL是你的查询语句,m_pConnection是你的数据库连接对象
//
m_pRecordSet.CreateInstance(__uuidof(Recordset));
try
{
    m_pRecordSet->Open((_bstr_t)strSQL, m_pConnection.GetInterfacePtr(),
                       adOpenDynamic, adLockOptimistic, adCmdText);
}
catch(_com_error *e)
{
    AfxMessageBox(e->ErrorMessage());
}if (!m_pRecordSet->IsEOF)
{
    CString str = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("RNAME"); 
    MessageBox(str);
}m_pRecordSet->Close();

解决方案 »

  1.   

    不建议使用这种形式获值:
    CString str = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("RNAME");  
    若字段中无值的话,就会出错建议用下面的方法来获值:
    _variant_t GetValue;
    GetValue=m_pRecordset->GetCollect("RNAME");
    if(GetValue.vt!=VT_NULL)
    {
        str=(LPCTSTR)_bstr_t(GetValue);
    }
    else
    {
        str="";
    }
      

  2.   


    //其中strSQL是你的查询语句,m_pConnection是你的数据库连接对象 m_pRecordSet.CreateInstance(__uuidof(Recordset)); 
    try 

        m_pRecordSet->Open((_bstr_t)strSQL, m_pConnection.GetInterfacePtr(), 
                           adOpenDynamic, adLockOptimistic, adCmdText); 

    catch(_com_error *e) 

        AfxMessageBox(e->ErrorMessage()); 
    } if (!m_pRecordSet->BOF) 

        m_pRecordSet->MoveFirst();
         CString str = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("RNAME");  
        MessageBox(str); 
    }