zoukankan      html  css  js  c++  java
  • VC ADO连接ACCESS步骤及错误处理

    步骤1:

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

    #import "C:Program FilesCommon FilesSystemadomsado15.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 FilesCommon FilesSystemadomsado15.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 FilesCommon FilesSystemadomsado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF")

    实际上win7和win xp的msado15.dll是不一样的,向下兼容,并且看到有人说win xp下的msado15.dll也不尽相同。如果想在win7下编译,并且能在xp下运行,可以拷贝

    win XP中C:Program FilesCommon FilesSystem路径下的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

  • 相关阅读:
    mysql数据库引擎myisam与innodb
    Java观察者模式的理解
    线程安全的单例模式
    线程相关
    java 线程读写锁
    原子变量AtomicInteger
    接口文档管理,版本管理工具,阿里RAP的windows下部署
    谷歌浏览器报错:跨域问题处理( Access-Control-Allow-Origin)_ 用于本地测试的快捷解决方法
    mysql bin-log日志记录
    阿里RDS中插入emoji 表情插入失败的解决方案
  • 原文地址:https://www.cnblogs.com/vipwtl/p/7771044.html
Copyright © 2011-2022 走看看