Directory listing of http: uap.unnes.ac.id ebook biblebook Visual Basic 6 com & Library Books

11
C H A P T E R

The ADO
Object M odel









In This Cha pter

I

n this part o f the bo o k, I’m go ing to turn my attentio n fro m
the to o ls that c an make it easier to build yo ur applic atio ns
and fo c us o n the underlying o bjec t mo del that they use. Over

time, I fo und that using the ADO o bjec ts direc tly is o ften easier
than using the to o ls. In this c hapter, I’m go ing to present a brief
o verview o f the ADO o bjec t mo del and explain ho w the vario us
o bjec ts fit to gether. In the rest o f Part III, I’ll dig into eac h o f the
key o bjec ts and sho w yo u ho w easy they are to use.

Introducing ActiveX
Data Objects 2.5
The Ac tiveX Data Objec ts (ADO) is Mic ro so ft’s way o f implementing Unive rsal Data Acce ss. Universal Data Ac c ess allo ws
yo u to use the same high-speed interfac e fo r bo th relatio nal
and no n-relatio nal data, while pro viding an easy-to -use, language-independent interfac e.

The ADO object model
ADO c arries o ver so me o f the o bjec ts used in the o lder DAO
and RDO o bjec t mo dels disc ussed in Chapter 6. Ho wever, it
uses a c o mpletely different appro ac h that remo ves the stric t
hierarc hy required when using the o lder o bjec t mo dels (see
Figure 11-1 and Table 11-1).

Intro ducing ADO 2 .5

Using ADO o bjects
W o rking with the
ADO Erro rs co llectio n









202

Part II ✦ Beginning Database Programming

Command

Parameters


Parameter

Properties

Property

Errors

Error

Properties

Property

Connection

Record

Fields


Field

Properties

Property

Properties

Property

Recordset

Fields

Properties

Field

Property


Stream

Figure 11-1: Presenting the ADO object m odel

There are three main o bjec ts in the ADO o bjec t mo del that are used fo r database
ac c ess. The Connection o bjec t pro vides a path to the data so urc e. The Command
o bjec t c o ntains the info rmatio n nec essary to exec ute an SQL Statement o r a sto red
pro c edure. The Recordset o bjec t c o ntains the results fro m a query. The Command
and Recordset o bjec ts have the ability to c reate an implic it Connection o bjec t if
desired, so yo u c an ac c ess a set o f rec o rds o r perfo rm a database func tio n by c reating o nly a single o bjec t.

Chapter 11 ✦ The ADO Object M odel

Table 11-1
ADO Objects
Object

Description

Command


Executes an SQL Statem ent or stored procedure.

Connection

Used to m anage the inform ation necessary to connect to a database or
other OLE DB data provider.

Error

Contains inform ation about a specific error.

Errors

Contains a collection of Error objects.

Field

Contains inform ation about a specific field in the database.


Fields

Contains a collection of Field objects.

Parameter

Holds inform ation that is passed to or returned from a stored procedure
or param eterized query.

Parameters

Contains a collection of Parameter objects.

Property

Contains a dynam ic property that is defined by the OLE DB data
provider.

Properties


Contains a collection of Property objects.

Record

Represents a row in Recordset object or a file or an e-m ail m essage.

Recordset

Used to m anage the set of row s generated by a query operation.

Stream

Represents a stream of binary data or text.

The Record and Stream o bjec ts are used primarily to suppo rt ac c ess to no ndatabase reso urc es. The Record o bjec t represents either a ro w in a Recordset o r a
file o r an e-mail message. A Stream o bjec t pro vides the fac ilities to manipulate the
data in a file o r an e-mail message.

New in ADO 2.5
If yo u are familiar with the previo us versio ns o f ADO, here’s a quic k intro duc tio n to

the new features.

The Record object
The Record o bjec t is used to represent info rmatio n suc h as direc to ries and files in
a file system, and fo lders and messages in an e-mail system. A Record o bjec t c an
also be used to represent a ro w fro m a Recordset o bjec t.

203

204

Part II ✦ Beginning Database Programming

The Stream object
The Stream o bjec t is used to read and write binary info rmatio n to and fro m files
and messages asso c iated with the Record o bjec t.

URLs
URLs may no w be used in plac e o f c o nnec tio n strings (see the Connection o bjec t)
and c o mmand text (see the Command o bjec t) as an alternative to using traditio nal

c o nnec tio n strings and c o mmand text.

New properties and methods
Several new pro perties and metho ds have been added to the ADO library. The Mode
pro perty is available in the Connection, Record, and Stream o bjec ts and desc ribes
the permissio ns available fo r mo difying data. The rest o f the new pro perties and
metho ds are used by the new Stream and Recordset o bjec ts to manage files and
e-mail messages.

Introducing ActiveX Data Objects Extensions
While no t exac tly new in ADO 2.5, the Ac tiveX Data Objec ts Extensio ns (ADOX)
gives yo u the c apability to c reate and mo dify a database’s struc ture (also kno wn as
a sche ma ) and maintain sec urity thro ugh an o bjec t-o riented appro ac h. Sinc e these
o bjec ts are independent o f any single data pro vider, it is po ssible to build a generalpurpo se pro gram that wo uld wo rk with any data pro vider.
The ADOX o bjec t mo del (see Figure 11-2 and Table 11-2) was develo ped as an o bjec to riented way fo r so meo ne to ac c ess a database sc hema. The sc hema pro vides ac c ess
to all o f the po ssible o bjec ts in a database: tables, indexes, sto red pro c edures and
views, as well as sec urity info rmatio n.
The Catalog is the ro o t o bjec t. All o ther o bjec ts c an be referenc ed fro m the
Catalog. Beneath the Catalog o bjec t are the five main c o llec tio ns o f database
o bjec ts, the Tables c o llec tio n, the Groups c o llec tio n, the Users c o llec tio n, the

Procedures c o llec tio n, and the Views c o llec tio n.
The Tables c o llec tio n do c uments the physic al struc ture o f the data. Fro m the
Table o b jec t, yo u c an find o ut the details o f the struc ture b y examining the
Columns, Indexes, and Keys c o llec tio ns. No te that the Index and Key o b jec ts
also referenc e the Columns c o llec tio n. Ho wever, b efo re yo u c an add a Column
o b jec t to either the Index o r Key o b jec t, it must already exist in the Table o b jec t.

Chapter 11 ✦ The ADO Object M odel

Catalog

Tables

Table
Columns

Column

Indexes

Index
Columns

Keys
Groups

Groups

Key
Columns

Group

Column

Users

User

Users

User

Column

Group

Procedures

Procedure

View s

View

Figure 11-2: Presenting the ADOX object m odel

Table 11-2
ADOX Objects
Object

Description

Catalog

Contains the schem a definition of the data source. This is the base object
in ADOX.

Column

Contains inform ation about a specific colum n from a table, index, or key.

Columns

Contains the Column definitions of a table, index, or key.
Continued

205

206

Part II ✦ Beginning Database Programming

Table 11-2 (continued)
Object

Description

Group

Contains inform ation about a security group.

Groups

Contains all of the security Group objects in the catalog.

Index

Contains inform ation about an index for a database table.

Indexes

Contains the set of indexes associated w ith a table.

Key

Contains the definition of a key field from a table.

Keys

Contains the set of Key objects associated w ith a table.

Procedure

Contains inform ation related to a stored procedure.

Procedures

Contains the set of stored procedures.

Table

Contains the definition of a table, including things like colum ns, indexes,
and keys.

Tables

Contains the set of tables in the Catalog.

User

Contains security inform ation about a user account and its database
perm issions.

Users

Contains the set of users associated w ith the Catalog.

View

Contains the schem a definition of a database view.

Views

Contains the collection of View objects in the Catalog.

The Groups c o llec tio n c o ntains the set o f sec urity gro ups. Eac h Group o bjec t c o ntains a referenc e to the c o llec tio n o f Users that are in that partic ular gro up. Befo re
yo u c an add a user to the Users c o llec tio n, it must already exist in the Users gro up
that is direc tly beneath the Catalog o bjec t.
The Users c o llec tio n is similar to the Groups c o llec tio n, exc ept that the users
asso c iated with the datab ase are sto red in the Users c o llec tio n, while the gro ups
asso c iated with a single User are sto red in the Groups c o llec tio n. As yo u might
expec t, in o rder to add a gro up to the Groups c o llec tio n, yo u must first define it
in the Groups o b jec t that is direc tly b eneath the Catalog o b jec t.
The Procedures c o llec tio n do c uments the set o f sto red pro c edures available in the
database. A single Procedure o bjec t c o ntains all o f the info rmatio n asso c iated with
a spec ific pro c edure. It also inc ludes a referenc e to a Command o bjec t that c an be
used to exec ute the sto red pro c edures.

Chapter 11 ✦ The ADO Object M odel

The Views c o llec tio n do c uments the set o f views in the database. Eac h view is
defined in the View o bjec t. Like the Procedure o bjec t, a referenc e to a Command
o bjec t that will retrieve the values fro m the view is also inc luded.

Basic ADO Programming
Writing a database pro gram using the ADO o bjec t mo del is mo re wo rk than using
the ADO Data Co ntro l o r the Data Enviro nment Designer, but it is far mo re flexible
in the lo ng run.

Connecting to the data source
The first step in building an ADO based applic atio n is c o nnec ting to the database.
The key to this step is c reating a c o nnec tio n string that inc ludes key info rmatio n,
suc h as the data pro vider, the lo c atio n o f the data, and yo ur user name and passwo rd. In additio n, yo u may need to pro vide additio nal info rmatio n, suc h as the
default database name that is required fo r a spec ific data pro vider.
The Connection o bjec t uses the c o nnec tio n string to c reate a way fo r yo ur pro gram
to c o mmunic ate to the database. Onc e o pened, all database func tio ns perfo rmed by
yo ur applic atio n must referenc e the Connection o bjec t, either explic itly o r implic itly.
In additio n to pro viding a gateway to a data so urc e, the Connection o b jec t also
serves as a c o mmo n plac e to rec o rd erro r info rmatio n b y using the Errors c o llec tio n. The Errors c o llec tio n c o ntains info rmatio n ab o ut the mo st rec ent erro r
enc o untered. If a new erro r o c c urs, the Errors c o llec tio n will b e c leared b efo re
any new info rmatio n is added. It’s impo rtant to no te that a suc c essful o peratio n
will no t c lear the Errors c o llec tio n. Thus, the Errors c o llec tio n may no t apply to
the mo st rec ently exec uted o peratio n. The o nly way to b e c ertain is to c lear the
Errors c o llec tio n after handling the erro r o r immediately b efo re perfo rming a
c ritic al o peratio n.
CrossReference

The Connection object is discussed in detail in Chapter 12.

Executing a command
Onc e yo u have a c o nnec tio n to the data so urc e, yo u may issue c o mmands to perfo rm
database o peratio ns. The c o mmands yo u issue are defined in a Command o bjec t, using
the CommandText and CommandType pro perties. There are three basic types o f c o mmands: SQL Statements, table names, and sto red pro c edures.

207

208

Part II ✦ Beginning Database Programming

Onc e a Command is defined and yo u have an o pen Connection to a data so urc e, yo u
may exec ute the Command. The c o mmand may use a set o f Parameters to c o ntro l
ho w it wo rks. This info rmatio n is kept in the Parameters c o llec tio n. Parameters c an
c o ntain values that are passed to the data so urc e and values that are returned fro m
the data so urc e. In additio n to returning values thro ugh a parameter, a Command may
also return a Recordset o bjec t, c o ntaining a set o f ro ws that were retrieved fro m the
data so urc e.
A Recordset will always be returned if yo u exec ute an SQL Select Statement o r
spec ify a table name fo r CommandText. All o ther SQL Statements will no t return
a Recordset. Sto red pro c edure, o n the o ther hand, may o r may no t return a
Recordset depending o n ho w the sto red pro c edure was written.
CrossReference

The Command object is discussed in detail in Chapter 13.

Playing with Recordsets
A Recordset o bjec t is the way yo u ac c ess the ac tual data sto red in yo ur database.
The Recordset maintains a po inter to the c urrent ro w that c an be manipulated
thro ugh the vario us metho ds available in the o bjec t. Yo u c an mo ve to the next ro w
o r the previo us ro w, using the MoveNext and MovePrevious metho ds. Yo u c an jump
to the first ro w o r to the last ro w with the MoveFirst and MoveList metho ds. Yo u
c an save the c urrent rec o rd po inter and later resto re it using the Bookmark pro perty.
Ac c ess to the info rmatio n in a partic ular ro w is thro ugh the Fields c o llec tio n. Eac h
individual c o lumn in the Recordset is represented by a Field o bjec t. Yo u c an ac c ess
the c urrent value o f the c o lumn tho ugh the Value pro perty. If the Recordset was
o pened with update ac c ess, yo u c an c hange the value o f the c o lumn by assigning a
value to the Value pro perty. Yo u c an then save the c hanges to yo ur database by
using the Update metho d o r disc ard the c hanges by using the CancelUpdate
metho d.
Other func tio ns are available to help yo u lo c ate a partic ular ro w in the Recordset
using the Find metho d , o r yo u c an filter the ro ws c urrently available in the
Recordset to eliminate unwanted ro ws with the Filter pro perty. Yo u c an also
refresh the data in the Recordset by using the Requery o r Resync metho d.
CrossReference

The Recordset object is discussed in detail in Chapters 14, 15, and 16.

Chapter 11 ✦ The ADO Object M odel

Thoughts on ADO
Data bound controls are only really useful if you have a visible object. Som e types of Visual
Basic program s, such as an IIS Application or a COM+ transaction, don’t have a visual com ponent, so you can’t use bound controls. In general, I find the ADO object m odel m uch easier to use than either the DAO or RDO object m odels. All you need to do is establish a
connection to the database and open a recordset and you’re ready to go.
When ADO w as first released, it didn’t contain an object-oriented w ay to create schem as
and for som e reason, the Microsoft Jet database (the core database used in Access and frequently used by Visual Basic program m ers), didn’t include the SQL Statem ents needed to
define schem as. The only w ay to build a Jet database w as to either use the DAO objects
provided for schem a definition or use a utility program such as the Visual Data Manager or
Access to create an em pty database. Because Microsoft w anted current DAO users to m ove
up to ADO, they created the ADOX objects. Now there isn’t any reason for som eone to continue using DAO.

Summary
In this c hapter yo u learned the fo llo wing:

✦ Yo u c an use the ADO Connection o bjec t to ac c ess a database.
✦ Yo u c an use the ADO Command o bjec t to run SQL statements and sto red pro c edures o n a database server.

✦ Yo u c an use the ADO Recordset o bjec t to retrieve info rmatio n fro m a
database and update the info rmatio n sto red in the database.







209