Home Database Mysql Tutorial VC6.0中使用ADO操作Access数据库

VC6.0中使用ADO操作Access数据库

Jun 07, 2016 pm 03:39 PM
access use operate database

由于我的程序只是简单的储存网址和标题,Access小而简单,所以就选择Access作为本小软件的数据库,并采用ADO访问数据库。 以下数据库内容摘自孙鑫老师的VC20讲第20课数据库访问的PPT 数据库访问技术 1. ODBC(Open Database Connectivity),开放数据库互连。O

       
    由于我的程序只是简单的储存网址和标题,Access小而简单,所以就选择Access作为本小软件的数据库,并采用ADO访问数据库。
以下数据库内容摘自孙鑫老师的”VC20讲第20课数据库访问的PPT”
数据库访问技术
1. ODBC(Open Database Connectivity),开放数据库互连。ODBC是上个世纪八十年代末九十年代初出现的技术,它为编写关系数据库的客户软件提供了一种统一的接口。 ODBC提供一个单一的API,可用于处理不同数据库的客户应用程序。使用ODBC API的应用程序可以与任何具有ODBC驱动程序的关系数据库进行通信。

2. DAO(Data Access Object),数据访问对象。DAO就是一组Microsoft Access/Jet数据库引擎的COM自动化接口。 DAO不像ODBC那样是面向C/C++程序员的,它是微软提供给Visual Basic开发人员的一种简单的数据访问方法,用于操纵Access数据库。

3. RDO(Remote Data Object),远程数据对象。由于RDO直接调用ODBC API(而不是像DAO那样通过Jet引擎),所以,可以为使用关系数据库的应用程序提供更好的性能。

4. OLE DB,对象链接与嵌入数据库。 OLE DB在两个方面对ODBC进行了扩展。首先, OLE DB提供了一个数据库编程的COM接口;第二, OLE DB提供了一个可用于关系型和非关系型数据源的接口。 OLE DB的两个基本结构是OLE DB提供程序(Provider)和OLE DB用户程序(Consumer)。

5. ADO(ActiveX Data Object),ActiveX数据对象,它建立在OLE DB之上。ADO是一个OLE DB用户程序。使用ADO的应用程序都要间接地使用OLE DB。ADO简化了OLE DB,提供了对自动化的支持,使得像VBScript这样的脚本语言也能够使用ADO访问数据库。

ADO的三个核心对象
1.Connection对象
    Connection对象表示了到数据库的连接,它管理应用程序和数据库之间的通信。 Recordset和Command对象都有一个ActiveConnection属性,该属性用来引用Connection对象。

2.Command对象
    Command对象被用来处理重复执行的查询,或处理需要检查在存储过程调用中的输出或返回参数的值的查询。

3.Recordset对象
   Recordset对象被用来获取数据。 Recordset对象存放查询的结果,这些结果由数据的行(称为记录)和列(称为字段)组成。每一列都存放在Recordset的Fields集合中的一个Field对象中。

用VC连接Access的方法如下:

1.将msado15.dll动态库拷贝到你应用程序所在的文件夹,msado15.dll一般放在C:/Program Files/Common Files/System/ado目录下,你也可以通过Windows的收搜功能进行收搜。
在头文件StdAfx.h中加上#import "msado15.dll" no_namespace rename ("EOF", "adoEOF")
因为记录集的结尾是EOF,而文件的结尾也是EOF,所以这里为了避免冲突,将其重新命名为adoEOF。编译程序,这时在你的Debug目录下多出现两 个文件:msado15.tlh和msado15.tli,这是msado15.dll在编译时自动生成的,有时候可以帮助你了解一些函数、常数的含义。

2.初始化ADO并连接数据库
Ado实际上是一个COM组件,所以我们在使用它之前要进行初始化,结束后要卸载。
加上一个成员变量 _ConnectionPtr m_pConnection; //连接数据库的智能指针
// 初始化—连接数据库
// 初始化OLE/COM库环境
::CoInitialize(NULL);
……
hr=m_pConnection.CreateInstance(__uuidof(Connection)); //实例化连接对象
……
m_pConnection->ConnectionString= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=helper.mdb"; //连接数据库
//如果Access数据库中设置了密码,那么此句应为
//m_pConnection->ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=helper.mdb;Persist Security Info=False;Jet OLEDB:Database Password=123456";
还有这里涉及一个字符串过长的问题,该字符串在一行中写不下去,需多行输出。
如CString str=”123456789”; 分两行写为:
Str=”123456”/ //为清楚末尾加 / 在VC中不加也可以,但不要有分号
“789”;

……
m_pConnection->ConnectionTimeout=10; //设置等待连接打开的时间为10秒
hr=m_pConnection->Open("","","",adConnectUnspecified);//打开连接
……(失败、异常处理)

3.数据库读取、修改、删除
……
CString strSQL=”select * from stock”;
_RecordsetPtr pQueryRecordset;
……
hr=pQueryRecordset.CreateInstance(__uuidof(Recordset));
……
pQueryRecordset->Open(_variant_t(strSQL),m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockOptimistic,adCmdText); //打开数据库
……
if((pQueryRecordset->BOF)&&(pQueryRecordset->adoEOF))//记录集没有记录
{ …… }
else
{
_variant_t var; CString strValue; int curItem;
……
while(!pQueryRecordset->adoEOF)
{
var = pQueryRecordset->GetCollect("title");
strValue = (LPCSTR)_bstr_t(var);
ListCtrl.InsertItem(curItem,strValue);
…… //这里的多少取决你的字段…….
pQueryRecordset->MoveNext();
curItem++;
}
}
……
修改
Recordset->PutCollect("visit", (LPCTSTR)visit );
Recordset->Update();
增加
Recordset->AddNew();
Recordset->PutCollect("visit", (LPCTSTR)visit );
Recordset->Update();

删除表
strSQL=”delete from stock”;
Recordset->Open(_variant_t(strSQL),_variant_t((IDispatch*)m_pConnection,true),
adOpenStatic,adLockOptimistic,adCmdText);

4.撤消数据库连接,卸载ADO
m_pConnection->Close(); // 释放环境
::CoUninitialize();

另外,由于Access会对你的数据进行排序,所以你读出的数据次序不一定会跟你写进Access时一致。
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

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)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

What are the benefits of multithreading in c#? What are the benefits of multithreading in c#? Apr 03, 2025 pm 02:51 PM

The advantage of multithreading is that it can improve performance and resource utilization, especially for processing large amounts of data or performing time-consuming operations. It allows multiple tasks to be performed simultaneously, improving efficiency. However, too many threads can lead to performance degradation, so you need to carefully select the number of threads based on the number of CPU cores and task characteristics. In addition, multi-threaded programming involves challenges such as deadlock and race conditions, which need to be solved using synchronization mechanisms, and requires solid knowledge of concurrent programming, weighing the pros and cons and using them with caution.

How to avoid third-party interfaces returning 403 errors in Node environment? How to avoid third-party interfaces returning 403 errors in Node environment? Apr 01, 2025 pm 02:03 PM

How to avoid the third-party interface returning 403 error in the Node environment. When calling the third-party website interface using Node.js, you sometimes encounter the problem of returning 403 error. �...

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Unable to log in to mysql as root Unable to log in to mysql as root Apr 08, 2025 pm 04:54 PM

The main reasons why you cannot log in to MySQL as root are permission problems, configuration file errors, password inconsistent, socket file problems, or firewall interception. The solution includes: check whether the bind-address parameter in the configuration file is configured correctly. Check whether the root user permissions have been modified or deleted and reset. Verify that the password is accurate, including case and special characters. Check socket file permission settings and paths. Check that the firewall blocks connections to the MySQL server.

How to use sql if statement How to use sql if statement Apr 09, 2025 pm 06:12 PM

SQL IF statements are used to conditionally execute SQL statements, with the syntax as: IF (condition) THEN {statement} ELSE {statement} END IF;. The condition can be any valid SQL expression, and if the condition is true, execute the THEN clause; if the condition is false, execute the ELSE clause. IF statements can be nested, allowing for more complex conditional checks.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to efficiently obtain component_verify_ticket in EasyWechat 5.5? How to efficiently obtain component_verify_ticket in EasyWechat 5.5? Apr 01, 2025 pm 12:42 PM

Get ComponentVerify in EasyWechat5.5...

See all articles