VC ADO连接ACCESS步骤及错误处理

广大难题:

步骤1:

链接: https://pan.baidu.com/s/1cbI4t4 密码:
ne39

1.win7下编译,xp下无法利用,会报错

#import “C:\Program Files\Common Files\System\ado\msado15.dll”
no_namespace rename(“EOF”,”adoEOF”)rename(“BOF”,”adoBOF”)

win7 ADO下载:

#import “C:\Program Files\Common Files\System\ado\msado15.dll”
no_namespace rename(“EOF”,”adoEOF”)

xp ADO下载:

// ADOConn.cpp: implementation of the ADOConn class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "SuperMarket.h"

#include "ADOConn.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ADOConn::ADOConn()
{

}

ADOConn::~ADOConn()
{

}

void ADOConn::OnInitADOConn()
{
    ::CoInitialize(NULL);// 此句很重要,不要遗漏
    try
    {
       m_pConnection.CreateInstance("ADODB.Connection");

       CString path = AfxGetApp()->m_lpCmdLine;
       if (path.IsEmpty())
       {
            m_pConnection->Open(_bstr_t(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=fctest.mdb;Persist Security Info=False")),"","",adOpenUnspecified);
       }
       else
       {
           int pos = path.ReverseFind('\\');
           path = path.Left(pos);
//注:fctest.mdb是我的本地数据库名        
           CString mdbPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ path +"\\fctest.mdb;Persist Security Info=False";

            m_pConnection->Open((_bstr_t)mdbPath,"","",adOpenUnspecified);
       }

    }
    catch(_com_error e)
    {  
        AfxMessageBox(e.Description());
        return;
    }
}

//执行SQL语句
BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
    try
    {
        if(m_pConnection==NULL)
            OnInitADOConn();
        m_pConnection->Execute(bstrSQL,NULL,adCmdText);
        return TRUE;
    }
    catch(_com_error e)
    {
//        e.Description();
        AfxMessageBox(e.Description());
        return FALSE;
    }
}

//取消和数据库的链接
void ADOConn::ExitConnect()
{
    if(m_pRecordset != NULL)
        m_pRecordset->Close();
    m_pConnection->Close();
    ::CoUninitialize();// 此句很重要,不要遗漏
}

//读取数据集
_RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL)
{
    try
    {
        if(m_pConnection==NULL)
              OnInitADOConn();
        m_pRecordset.CreateInstance(__uuidof(Recordset));
        m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
    }
    catch(_com_error e)
    {
        e.Description();
    }

    return m_pRecordset;
}
        ADOConn ado;
    ado.OnInitADOConn();
    CString sql;
    sql.Format("delete from test where id = 5");
    ado.ExecuteSQL((_bstr_t)sql);

    ado.ExitConnect();    

示范源码下载:

应用形式:

        ADOConn ado;
    ado.OnInitADOConn();
    CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST1);
    pListCtrl->DeleteAllItems();
    int n = 0;
    CString sql;
    sql.Format("select * from test where id > 0");
    ado.m_pRecordset = ado.GetRecordSet((_bstr_t)sql);//读取数据集
    while(!ado.m_pRecordset->adoEOF)
    {
        CString i1,i2,i3,i4;
        i1 = (char*)(_bstr_t)ado.m_pRecordset->GetCollect("id");
        i2 = (char*)(_bstr_t)ado.m_pRecordset->GetCollect("name");
        i3 = (char*)(_bstr_t)ado.m_pRecordset->GetCollect("age");
        i4 = (char*)(_bstr_t)ado.m_pRecordset->GetCollect("zhiye");

        pListCtrl->InsertItem(n,i1);
        pListCtrl->SetItemText(n,1,i2);
        pListCtrl->SetItemText(n,2,i3);
        pListCtrl->SetItemText(n,3,i4);
        n++;
        ado.m_pRecordset->MoveNext();
    }
    ado.ExitConnect();    

删除:

步骤3:

链接: https://pan.baidu.com/s/1sljv6ax
密码: pcn9

累加文书ADOConn.cpp

步骤2:

2.开始化失利,调试时意识_ConnectionPtr
指针为空

增加:

查询:

实则win7和win xp的msado15.dll是不雷同的,向下包容,并且探望有人说win
xp下的msado15.dll也不完全一样。若是想在win7下编译,并且能在xp下运作,可以拷贝

dialog中有IDC_LIST1的list。

在StdAfx.h的#include语句之后添加

链接: https://pan.baidu.com/s/1cde7h0 密码:
1xu8

原因:当时不曾加::CoInitialize(NULL);及::CoUninitialize();语句。

添加文书ADOConn.h

ADOConn ado;
    ado.OnInitADOConn();
    CString sql;
    sql.Format("update test set name = '%s' where name = '%s' ","潘把子","老潘");
    ado.ExecuteSQL((_bstr_t)sql);

    ado.ExitConnect();
// ADOConn.h: interface for the ADOConn class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_ADOCONN_H__5FB9A9B2_8D94_44F7_A2DA_1F37A4F33D10__INCLUDED_)
#define AFX_ADOCONN_H__5FB9A9B2_8D94_44F7_A2DA_1F37A4F33D10__INCLUDED_
#include <icrsint.h>
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF")
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class ADOConn  
{
public:
    _ConnectionPtr m_pConnection;
    _RecordsetPtr m_pRecordset;
public:
    _RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);
    ADOConn();
    virtual ~ADOConn();

    void OnInitADOConn();
    BOOL ExecuteSQL(_bstr_t bstrSQL);
    void ExitConnect();
};

#endif // !defined(AFX_ADOCONN_H__5FB9A9B2_8D94_44F7_A2DA_1F37A4F33D10__INCLUDED_)

修改:

        ADOConn     ado;
    ado.OnInitADOConn();
    CString sql;
    sql.Format("insert into test(id,name,age,zhiye) values (%d,'%s',%d,'%s')",5,"老潘",23,"打手");
    ado.ExecuteSQL((_bstr_t)sql);

    ado.ExitConnect();    

win XP中C:\Program Files\Common
Files\System\路线下的ado文件夹到win7相同路线下(注意保存原来的win7
ado文件夹),那样在win7下编译的先后在win xp下也得以运行。win7 及 win
XP下的ado文件夹见附件。

化解办法:注意到代码里有

相关文章