跳转到内容

ActiveX Data Objects

本页使用了标题或全文手工转换
维基百科,自由的百科全书

这是本页的一个历史版本,由PhiLiP留言 | 贡献2009年7月1日 (三) 05:36 基本功能编辑。这可能和当前版本存在着巨大的差异。

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

架構

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

ADO包含一些顶层的对象:

  • 连接(Connection),代表到数据库的连接
  • 记录集(Recordset),代表数据库记录的一个集合
  • 命令(Command),代表一个SQL命令
  • 记录(Record),代表数据的一个集合
  • 流(Stream),代表数据的顺序集合
  • 错误(Error),代表数据库访问中产生的意外
  • 字段(Field),代表一个数据库字段
  • 参数(Parameter),代表一个SQL参数
  • 属性(Property),保存对象的信息

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

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

基本功能

使用 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)

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)

參見

外部链接