我写了一个根据字段名取数据的函数:
CString CCMD::GetRecord(CString strColName, _RecordsetPtr pRs)
{
//将指定的查询的一列的一个记录转换为一个字符串
_variant_t vValue;
CString strValue;
try
{
if (!pRs->EndOfFile)
{
vValue = pRs->GetCollect(_variant_t (strColName));//取得字段值
if(vValue.vt != VT_NULL)//不为空
{
vValue.ChangeType(VT_BSTR, NULL);
strValue = vValue.bstrVal;
return strValue;
}
else
return "";

//使用转换类型直接取得数据的字符串
}
else
return "";
}
catch (_com_error)
{
return "";
}
}
现在我想通过指定取值是在记录集的第几列来取值请问怎么实现:CString CCMD::GetRecord(int strNum, _RecordsetPtr pRs)
{//取第strNum列的值,并转换成CString类型
}
谢谢!

解决方案 »

  1.   

    vValue = pRs->GetCollect(1);//取得字段值
      

  2.   

    刚刚试了一下,直接vSendToID = m_pRecordset->GetCollect(4);这样写会报错
           “cannot convert parameter 1 from 'const int' to 'const class _variant_t &'
            Reason: cannot convert from 'const int' to 'const class _variant_t'”
    要写成vSendToID = m_pRecordset->GetCollect((long)4)转换一下类型,而且列和行一样是从0开始计数