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

步骤1:

在StdAfx.h的#include语句后补充加

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

步骤2:

丰富文书ADOConn.h

// 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_)

步骤3:

长文件ADOConn.cpp

// 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;
}

用方法:

dialog中有IDC_LIST1的list。

查询:

        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();    

增加:

        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();    

删除:

        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();
    CString sql;
    sql.Format("update test set name = '%s' where name = '%s' ","潘把子","老潘");
    ado.ExecuteSQL((_bstr_t)sql);

    ado.ExitConnect();

普遍问题:

1.win7下编译,xp下未可知下,会报错

解决办法:注意到代码里发

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

其实win7和win xp的msado15.dll是匪雷同的,向下兼容,并且探望有人说win
xp下的msado15.dll也不尽相同。如果想以win7下编译,并且能够在xp下运作,可以拷贝

win XP中C:\Program Files\Common
Files\System\途径下之ado文件夹到win7相同路线下(注意保留原来的win7
ado文件夹),这样在win7产编译的次于win xp下也足以运行。win7 及 win
XP下的ado文件夹见附件。

2.初始化失败,调试时发现_ConnectionPtr
指针为空

缘由:当时从未加::CoInitialize(NULL);及::CoUninitialize();语句。

示范源码下载:

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

xp ADO下载:

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

win7 ADO下载:

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

相关文章