我尝试下面这个程序段来给数据库添加一项,结果是:虽然编译通过,可以去到对话框的界面,但是当插入的时候,按插入键后弹出如下错误:
Unhandled exception in AdoRWAccess.exe(KERNEL32.DLL):0xE06D7363:Microsoft C++ Exception.
请问是什么错误,各位大侠可以帮我看看吗?
bool CAdoRWAccessDlg::OnInsert() 
{
_ConnectionPtr m_pConnection;
// 初始化COM,创建ADO连接等操作m_pConnection.CreateInstance(__uuidof(Connection));// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些意想不到的错误。jingzhou xu
try                 
{
// 打开本地Access库Demo.mdb
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Demo.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库Demo.mdb是否在当前路径下!");
return FALSE;
}      
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
m_pRecordset->Open("SELECT * FROM Demo",                // 查询Demo表中所有字段
(IDispatch*)m_pConnection.GetInterfacePtr(),  // 获取数据库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}       try
{
// 写入各字段值
m_pRecordset->MoveLast();
m_pRecordset->AddNew();
m_pRecordset->PutCollect("Name", _variant_t(m_Name));
m_pRecordset->PutCollect("Age", atol(m_Age));
m_pRecordset->Update(); AfxMessageBox("插入成功!");
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return FALSE;
}       //关闭一个库连接、则用Close方法关闭它并赋于它空值
if(m_pConnection->State)
        m_pConnection->Close();
        m_pConnection= NULL;
return FALSE;}

解决方案 »

  1.   

    m_pRecordset->MoveLast(); 
    这句注释掉实验一下。
      

  2.   

    m_pRecordset->Close(); 
    m_pRecordset= NULL; 
    断开连接之前
      

  3.   

    //断开连接之前要释放对象
    if(m_pRecordset!=NULL)
    {
    m_pRecordset->Close(); 
    m_pRecordset.Release();
    }
    if(m_pConnection != NULL)
    {
    m_pConnection->Close();
    m_pConnection.Release();
    }
      

  4.   

        根据4、5楼的大哥修改了程序,但是错误依然是:Unhandled exception in AdoRWAccess.exe(KERNEL32.DLL):0xE06D7363:Microsoft C++ Exception.
    真想不明白是哪里出了问题。哪位有更进一步的建议吗?
      

  5.   

    是不是少了
    //初始化COM
    AfxOleInit();这句
      

  6.   

    向数据库中加记录我会使用m_pConnection->Execute(bstrSQL,&RecordsAffected, adCmdText);
    其中bstrSQL是SQL语句,比如:insert into userinfo(...)(............
      

  7.   

    我已经在OnInitDialog()中加入初始化了。如下:
    if(!AfxOleInit())//这就是初始化COM库
    {
    AfxMessageBox("OLE初始化出错!");
    return FALSE;
    } 我测试的查看和删除都可以,就是添加的时候出现了那样的错误。
      

  8.   

    m_pRecordset->PutCollect("Age", _variant_t(atoi(m_Age))); 
    这样写试试
      

  9.   

      仍然不行,atoi不能编译通过,把atol则可以编译通过,运行后,想增加一项,输入姓名和年龄之后,按“添加”按钮,还是那样的错误:
    Unhandled exception in AdoRWAccess.exe(KERNEL32.DLL):0xE06D7363:Microsoft C++ Exception.
    真拿他没办法!
      

  10.   

    这个问题解决了,原来是这两句造成的:
    m_pRecordset->PutCollect("Name", _variant_t(m_Name)); 
    m_pRecordset->PutCollect("Age", atol(m_Age)); 
    我定义两个字符串变量,然后把编辑框的内容读入来,再把放到PutCollect中,这样就可以了。
                   CString strname,strage;
    GetDlgItemText(IDC_EDIT_NAME,strname);
    GetDlgItemText(IDC_EDIT_AGE,strage);
    // 写入各字段值

    m_pRecordset->AddNew();
    m_pRecordset->PutCollect("Namefull", _variant_t(strname));
    m_pRecordset->PutCollect("Agefull", _variant_t(atol(strage)));//
    但是,随之而来的却有另外一个问题出现了。我的删除无法删除了。
    明明列表框和数据库都没有空,当我选择一项来删除的时候,它总是提示我已经清空。看看我这样的删除程序是否有错:
    try

       CString strvoid;
       GetDlgItemText(IDC_LIST1,strvoid);
       int curSel = m_AccessList.GetCurSel();
       if(strvoid.IsEmpty())
       {
       AfxMessageBox("您的列表框已经清空!");
       }    else
       {   
    m_pRecordset->Delete(adAffectCurrent);  // 参数adAffectCurrent为删除当前记录---数据库中的
    m_pRecordset->Update();
    m_AccessList.DeleteString(curSel);// 删除当前记录---列表框中的

    AfxMessageBox("删除成功!");
       }      }
    catch(_com_error *e)
    {
    AfxMessageBox(e->ErrorMessage());

    }      
      

  11.   

      终于成功了!现在将所有的问题解决了!
    原来那样判断列表框是否是空是不行的。我采用下面的方式就可以了!
    谢谢大家的帮助!
    int curSel = m_AccessList.GetCurSel();
     
      if(curSel == LB_ERR)
       {
       AfxMessageBox("您的列表框已经清空!");
       }