编写一个访问数据库的简单程序:如下:
SDI模式
      StdAfx.h中包含        #import "c:\program files\common files\system\ado\msado15.dll" \
                    no_namespace \
                      rename ("EOF", "adoEOF")
     CMainFrame里声明 _ConnectionPtr m_pConnection;
        然后建立一个菜单,加入响应函数,内容如下
AfxOleInit();
m_pConnection.CreateInstance(__uuidof(Connection));
// m_pConnection->ConnectionString ="shop.mdb=e.udl";  // 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些意想不到的错误。jingzhou xu
try                 
{
// 打开本地Access库shop.mdb
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data     Source=shop.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库shop.mdb是否在当前路径下!"); } 
CCLUBINF AddClubDlg;
if(AddClubDlg.DoModal()==IDOK)
{
// srand( (unsigned)time( NULL ) );
MessageBox("即将保存数据");
      _RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));

// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些意想不到的错误。jingzhou xu
try
{
m_pRecordset->Open("SELECT * FROM Club",                // 查询Club表中所有字段
m_pConnection.GetInterfacePtr(),  // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);

// 写入各字段值
m_pRecordset->AddNew();
m_pRecordset->PutCollect("Club_Name","丁丁");
m_pRecordset->PutCollect("Club_Num", _variant_t("413491374891"));
m_pRecordset->PutCollect("Club_Sex", false);
m_pRecordset->Update();

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

}
if(m_pConnection->State)
        m_pConnection->Close();
m_pConnection= NULL;
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、AddClubDlg是我定义的一个用来让用户填信息的窗体,程序执行到MessageBox("即将保存数据");后,出现
Obnormal program termination错误而退出。。我以为是环境问题,就另建立一个窗口工程,用一个按钮来响应同样的事件,结果确依旧如此提示Runtime error,高手救救我!!!!

解决方案 »

  1.   

    用 
    CString sql="插入语句";
    m_pConnection->Execute((_bstr_t)sql,NULL,adCmdText);试试
      

  2.   

    程序执行完MessageBox("即将保存数据");后,也就是弹出这个对话框后,就直接出错,我对每条语句执行完都加了MessgeBox,后面的根本就没反应。
    也就是说一创建那个m_pRecordset就直接出错了。。
      

  3.   

    ::CoInitialize(NULL);用这个替换AfxOleInit(); m_pRecordset.CreateInstance(__uuidof(Recordset)); 
    这一句放到try里面。你估计拷贝的例子代码改的。问题不少。
      

  4.   

    inline HRESULT Recordset15::Open ( const _variant_t & Source, const _variant_t & ActiveConnection, enum CursorTypeEnum CursorType, enum LockTypeEnum LockType, long Options ) {
        HRESULT _hr = raw_Open(Source, ActiveConnection, CursorType, LockType, Options);
        if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
        return _hr;
    }
    单步跟进,提示在return _hr;这里发生内存异常
      

  5.   

    几个个思路1.感觉你的ADO组件版本比较老,到微软站点下载mdac 2.8 sp1 安装之后,再进行代码的调试
    2.暂时不用窗口程序,在控制台程序中调试下代码,通过了再运用到窗体中。
    3.不要使用动态游标,使用只读向前的游标和锁定类型,确认open成功了,在更换油表类型。
    4.游标位置要设置为客户端游标
    m_pConnection->CursorLocation = adUseClient;
      

  6.   


    void SQLAccess()
    {
        CoInitialize(NULL);
        HRESULT    hr = S_OK;    // Define connection string variables.      
        _bstr_t strCnn("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Administrator\\Desktop\\test.mdb;Persist Security Info=False");    // Define ADO object pointers.
        // Initialize pointers on define.
        _ConnectionPtr  pConnection = NULL;
        _RecordsetPtr   pRst  = NULL;
        try
        {
            // Open connection.
            pConnection.CreateInstance(__uuidof(Connection));
            pConnection->Open (strCnn, "", "", adConnectUnspecified);        // Please note that question , a question  represents a parameter
            _bstr_t strSQL("select * from table1;");        // Create command object.
            pRst.CreateInstance(__uuidof(Recordset));        pRst->CursorLocation = adUseClient;
            pRst->Open((_variant_t)strSQL,              // 查询Club表中所有字段 
                        pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针 
                        adOpenDynamic, 
                        adLockOptimistic, 
                        adCmdText);        pRst->AddNew(); 
            pRst->PutCollect(_variant_t("id"),_variant_t("test0001")); 
            pRst->PutCollect(_variant_t("employee_name"),_variant_t("name1")); 
            pRst->Update();         // Clean up objects before exit.
            if (pRst)
            {
                if (pRst->State == adStateOpen)
                    pRst->Close();
                pRst.Release();
            }        if (pConnection)
            {
                if (pConnection->State == adStateOpen)
                    pConnection->Close();
                pConnection.Release();
            }
        }
        catch (_com_error &e)
        {
            // Print Com errors.
            _bstr_t bstrSource(e.Source());
            _bstr_t bstrDescription(e.Description());
            TRACE(_T("Error\n"));
            TRACE(_T("\tCode = %08lx\n"), e.Error());
            TRACE(_T("\tErrorMessage = %s\n"), (LPCTSTR)e.ErrorMessage());
            TRACE(_T("\tSource = %s\n"), (LPCTSTR)bstrSource);
            TRACE(_T("\tDescription = %s\n"), (LPCTSTR)bstrDescription);
        }
        ::CoUninitialize();
    }这是一段调试通过的,你可以参考一下