Home Database Mysql Tutorial vc如何通过ADO操作Access数据库?

vc如何通过ADO操作Access数据库?

Jun 07, 2016 pm 03:04 PM
access how operate database pass

数据库 的应用在应用程序中有不可缺少的作用,今天就讲解在vc下 如何 通过 ADO 操作 Access 数据库 。 首先引入ADO库的定义文件,引用msado15.dll,这个文件在系统安装盘的program files\common files\system\ado文件夹下,只需将以下代码: #import c:\prog

 

数据库的应用在应用程序中有不可缺少的作用,今天就讲解在vc下如何通过ADO操作Access数据库

 

首先引入ADO库的定义文件,引用msado15.dll,这个文件在系统安装盘的program files\common files\system\ado文件夹下,只需将以下代码:

 

#import “c:\program files\common files\system\ado\msado15.dll” \

 no_namespace \

 rename (“EOF”, “adoEOF”)

 

添加到StdAfx.h下便可。 www.2cto.com

 

再次,便是初始化COM库,这个步骤通常在初始化窗口时实现,代码如下:

 

AfxOleInit();

 

第三步便是利用建立好的连接通过Connection、Command对象执行SQL命令,或利用Recordset对象取得结果记录集进行查询、处理等。

 

// 定义ADO连接、命令、记录集变量指针

 _ConnectionPtr m_pConnection;

 _CommandPtr  m_pCommand;

 _RecordsetPtr m_pRecordset;

 

数据库Demo.mdb为例,以下代码初始化各指针:

 

m_pConnection.CreateInstance(__uuidof(Connection));// 初始化COM,创建ADO连接等操作

 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;

 }

 

m_pRecordset.CreateInstance(__uuidof(Recordset));// 使用ADO创建数据库记录集

  // 在ADO操作中建议语句中要常用try…catch()来捕获错误信息,

 // 因为它有时会经常出现一些想不到的错误。

 try

 {

  m_pRecordset->Open(“SELECT * FROM DemoTable”,    // 查询DemoTable表中所有字段

       m_pConnection.GetInterfacePtr(),  // 获取库接库的IDispatch指针

       adOpenDynamic,

       adLockOptimistic,

       adCmdText);

 }

 catch(_com_error *e)

 {

  AfxMessageBox(e->ErrorMessage());

 }

 

其次便可进行查询、修改、删除等操作了。

 

请关注   李木空间  www.limou.net   更多MFC知识。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to disable background applications in Windows 11_Windows 11 tutorial to disable background applications How to disable background applications in Windows 11_Windows 11 tutorial to disable background applications May 07, 2024 pm 04:20 PM

How to disable background applications in Windows 11_Windows 11 tutorial to disable background applications

How to convert deepseek pdf How to convert deepseek pdf Feb 19, 2025 pm 05:24 PM

How to convert deepseek pdf

How does the Java reflection mechanism modify the behavior of a class? How does the Java reflection mechanism modify the behavior of a class? May 03, 2024 pm 06:15 PM

How does the Java reflection mechanism modify the behavior of a class?

How to cross-domain iframe in vue How to cross-domain iframe in vue May 02, 2024 pm 10:48 PM

How to cross-domain iframe in vue

Common exception types and their repair measures in Java function development Common exception types and their repair measures in Java function development May 03, 2024 pm 02:09 PM

Common exception types and their repair measures in Java function development

How to read dbf file in oracle How to read dbf file in oracle May 10, 2024 am 01:27 AM

How to read dbf file in oracle

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

Detailed tutorial on establishing a database connection using MySQLi in PHP

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos

See all articles