ADO

微软公司的ADO (ActiveX Data Objects) 是一个用于访问数据源的COM组件。它提供了编程语言和统一数据访问方式OLE DB的一个中间层。允许开发人员编写访问数据的代码而不用关心数据库是如何实现的,而只用关心到数据库的连接。访问数据库的时候,关于SQL的知识不是必要的,但是特定数据库支持的SQL命令仍可以通过ADO中的命令对象来执行。

 

1 架构

ADO被设计来继承微软早期的数据访问对象层,包括RDO (Remote Data Objects) DAOData Access Objects)。ADO1996年冬被发布。

 

ADO包含一些顶层的对象:

l  连接(Connection),代表到数据库的连接

l  记录集(Recordset),代表数据库记录的一个集合

l  命令(Command),代表一个SQL命令

l  记录(Record),代表数据的一个集合

l  流(Stream),代表数据的顺序集合

l  错误(Error),代表数据库访问中产生的意外

l  字段(Field),代表一个数据库字段

l  参数(Parameter),代表一个SQL参数

l  属性(Property),保存对象的信息

ADO组件的使用需要利用支持COM的高级语言,例如ASP中的VBScript或者Visual Basic,甚至微软的竞争对手Borland的产品Delphi,现在也支持使用ADO来访问数据库。

 

在新的编程框架.NET Framework中,微软也提供了一个面向Internet的版本的ADO,称为ADO.NET。其对象模型和传统ADO差别很大。

 

2 基本功能

使用 ADO 访问数据的一些基本步骤:

1.        创建连接对象去连结数据库(Create a connection object to connect to the database.

2.        创建记录集对象来取得数据(Create a recordset object in order to receive data in.

3.        打开连接(Open the connection

4.        在记录集中完成SQL语法的描述(Populate the recordset by opening it and passing the desired table name or SQL statement as a parameter to open function.

5.        对获取的数据进行搜索/处理操作。

6.        确定改变数据(Commit the changes you made to the data (if any) by using Update or UpdateBatch methods.

7.        关闭记录集(Close the recordset

8.        关闭连接(Close the connection

 

2.1 ASP 示例

下列的 ASP 示例使用 ADO "Phonebook" 表中选取 "Name" 字段,其中 "PhoneNumber" 等于 "555-5555"

 

dim myconnection, myrecordset, name

set myconnection = server.createobject("ADODB.Connection")

set myrecordset = server.createobject("ADODB.Recordset")

 

myconnection.open mydatasource

myrecordset.open "Phonebook", myconnection

myrecordset.find "PhoneNumber = '555-5555'"

name = myrecordset.fields.item("Name")

myrecordset.close

 

set myrecordset = nothing

set myconnection = nothing

 

这相当于下列的 ASP code,以 plain SQL 取代 Recordset object:

 

dim myconnection, myrecordset, name

set myconnection = server.createobject("ADODB.connection")

myconnection.open mydatasource

set myrecordset = myconnection.execute("SELECT Name FROM Phonebook WHERE PhoneNumber = '555-5555'")

name = myrecordset(0)

 

 

转载说明:本文转载自维基百科,网址:http://zh.wikipedia.org/zh-cn/ADO

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值