表中共有四列,想动态地把表中内容打印出来,先获取列名,再获取记录内容获取列名代码如下:CString Headstr;//列名字符串
for(long i=0;i<nColCount;i++)
{     Headstr.format("值为:%s",m_pRecordset->Fields->GetItem(_variant_t((long)i))->GetName());
     AfxMessageBox(Headstr);
     //这两句想看看用Headstr取出的列名是否正确,,结果显示为乱码!!为什么呢????     AfxMessageBox(m_pRecordset->Fields->GetItem(_variant_t((long)0))->GetName());
     //这句可正常输出列名
}问题如代码中所示,,求高人解答,??、应该如何写呢???

解决方案 »

  1.   

    检查工程设置,检查字节对齐、检查数据库内容
    GetItem(_variant_t((long)i))//这些写数组访问合适吗? 
      

  2.   

    查看是否为类型转换的问题。GetName()的返回值是?
      

  3.   


    表中的列名分别为ID(integer)、username(string)、old(integer)、note(integer),所以我觉得GetName()的返回值应该是字符串CString类型的,对不对呢?
      

  4.   

    获得字段信息:Fields * pField=NULL;
        try{
            m_pRecordset.CreateInstance("ADODB.Recordset");
            m_pRecordset->Open("select * from 通讯录表",
                _variant_t((IDispatch *)theApp.m_pConnection,
                true),
                adOpenDynamic,
                adLockOptimistic,
                adCmdText);
            m_pRecordset->get_Fields(&pField);//获得字段集
            long fldNum;
            pField->get_Count(&fldNum);//获得字段的数目
            
            BSTR        bstrColName;
            for(long i=0;i<fldNum;i++)
            {
                //获得字段名字
                pField->Item[i]->get_Name(&bstrColName);        
                CString strfName=bstrColName;
                //获得字段的类型
                xxx::DataTypeEnum fType;
                pField->Item[i]->get_Type(&fType);
                int type=fType;
                
                //获得字段的大小
                long lSize;
                pField->Item[i]->get_DefinedSize(&lSize);
           }
            pField->Release();
        }
        catch (_com_error *e) {
            CString  temp =e->ErrorMessage();
            AfxMessageBox("读取数据库失败");
        }