还有明天一天就要交拉 急  急  急 急 
希望有人可以帮帮我  ! 拜谢
程序目的: 完成对表中字段和字段值的查询按行输出成XML格式 如 <字段>字段值</字段>
                                                             <字段2>字段值2</字段2>
控件: 我自己把程序写出来了 有一个 BUTTON控件和一个ListBOx控件 
目的: 按下BUTTON 在LISTBOX中显示XML代码!
Project name :First
程序清单:
(1)在在 stdafx.h 加入如下代码引入 ADO 库定义文件
 //如果这个写在最开始编译的时候回出现Windows.h重复使用的错误所以要写到下面去
  
#import "c:\program files\common files\system\ado\msado15.dll" \
no_namespace \
rename ("EOF", "adoEOF")   (2)初始化COM库,在BOOL BOOL CFirstDlg::OnInitDialog()中添加AfxOleInit(); 
//有的说到::InitInstance()中添加AfxOleInit(); 我不知道区别不过我都试过没有错出现!在类 class CFirstDlg : public CDialog 中添加两个用于打开数据库连接和打开记录集的变量;   public:

_RecordsetPtr m_pRecordset;
    
_ConnectionPtr m_pConnection ;
(3)BOOL CFirstDlg::OnInitDialog()中添加数据库连接和记录集初始化代码
if (!AfxOleInit())
{
AfxMessageBox(IDP_OLE_INIT_FAILED);
}
else
{   
m_pConnection.CreateInstance(__uuidof(Connection));
try
{    
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db4.mdb","","",adModeReadWrite);
}
catch(_com_error e)
{
AfxMessageBox(e.ErrorMessage());
} m_pRecordset.CreateInstance(__uuidof(Recordset));
}
(4 给Listbox控件添加控件变量m_List;在按钮的单击事件中添加相应代码
 _bstr_t mStrSQL;
     HRESULT hr;
 long   i,ColCount;
     FieldsPtr fields=NULL;  //这个定义有错么?? 
 CString   strColName,strVar;
     BSTR      bstrColName;
     _variant_t var;  
 //打开记录集执行部分
    mStrSQL = "SELECT * FORM device";
m_pRecordset->Open(mStrSQL,               
m_pConnection.GetInterfacePtr(),  
adOpenDynamic,
adLockOptimistic,
adCmdText);
    
hr = m_pRecordset->get_Fields (&fields); //得到记录集的字段集和

    if(SUCCEEDED(hr)) 
    fields->get_Count(&ColCount);    
    while(!m_pRecordset->adoEOF)
{
for(i=0;i<ColCount;i++)
{
 fields->Item[i]->get_Name(&bstrColName); //得到记录集中的字段名 如果I 是1话 是取的字段列的第一个字段名么???
 strColName = bstrColName;
 var = m_pRecordset->GetCollect(_variant_t(strColName));//取得字段值
 strVar = (LPCSTR)_bstr_t(var);
  m_List.AddString("<"+strColName+">"+strVar+"</"+strColName+">\n");  //输出XML格式
}
    m_pRecordset->MoveNext();  //下一个
}
//这里是不是要释放下fields指针啊??
m_pRecordset->Close();
    m_pConnection->Close();
}  编译通过拉 可是出现了Debug Asserts Failed         File:oleinit.cpp
   line:54
我查了查说是内存地址访问出了问题 !! 
还有就是像这个循环取得字段和字段值可以不??  想问问有更加简单的方法取字段名和字段值 
同事说有 不需要这个两个循环 可是我找不到那个函数!!
谢谢拉   看在我打这么多的份上帮忙看一下吧 如果可以解决 就谢谢拉

解决方案 »

  1.   

    编译通过拉 可是出现了Debug Asserts Failed         File:oleinit.cpp
       line:54
    ======看一下,这一行是在哪个类的哪个函数中?这个非常重要
      

  2.   

    这个错误我查过拉 是因为内存地址访问错误! 不知道是那个指针发生了错误!
    今天要交拉!还有就是你忽略这个错误的话 执行的时候也会发生abnormal program termination错误 也是关于内存的!!我再调试下吧
      

  3.   

    还有就是上面那两个循环可以完成我想要的结果么 
    如   学号  年龄   姓名    性别
         001    23     张三    男
         002    21     李四    男
         003    20     王五    男
         004    22     陈六    男要的结果是
    <学号>001</学号>
    <年龄>23</年龄>
    < 姓名>张三< /姓名>
    <性别>男</性别>
    <学号>002</学号>
         .
         .
         .
         .
         .
      

  4.   

    catch(_com_error e)//这里没报错?
    {
    AfxMessageBox(e.ErrorMessage());
    } m_pRecordset.CreateInstance(__uuidof(Recordset));
    catch( _com_error &e)
      

  5.   

     var = m_pRecordset->GetCollect(_variant_t(strColName));//取得字段值
    if (var.vt != VT_NULL)
    {
     strVar = (LPCSTR)_bstr_t(var);
      m_List.AddString("<"+strColName+">"+strVar+"</"+strColName+">\n");  //输出XML格式
    }
    else  
       m_List.AddString("<"+strColName+"></"+strColName+">\n");  //输出XML格式