• Tidak ada hasil yang ditemukan

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

N/A
N/A
Protected

Academic year: 2017

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

Copied!
10
0
0

Teks penuh

(1)

The ADO

Object M odel

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 imple-menting 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, lan-guage-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) .

1 1

1 1

In This Cha pter

Intro ducing ADO 2 .5

Using ADO o bjects

W o rking with the ADO Erro rs co llectio n

(2)

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 Connectiono 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 Recordseto bjec t c o ntains the results fro m a query. The Command

and Recordseto bjec ts have the ability to c reate an implic it Connectiono 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 reat-ing o nly a sreat-ingle o bjec t.

Command

Parameters

Properties

Parameter

Property

Connection

Errors

Properties

Error

Property

Record

Fields

Properties Field

Property

Fields Field Recordset

Stream

Properties Property

(3)

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 Errorobjects.

Field Contains inform ation about a specific field in the database.

Fields Contains a collection of Fieldobjects.

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

Parameters Contains a collection of Parameterobjects.

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

Properties Contains a collection of Propertyobjects.

Record Represents a row in Recordsetobject 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 Recordand Streamo bjec ts are used primarily to suppo rt ac c ess to no n-database reso urc es. The Recordo bjec t represents either a ro w in a Recordseto r a file o r an e-mail message. A Streamo 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

(4)

The Stream object

The Streamo bjec t is used to read and write binary info rmatio n to and fro m files and messages asso c iated with the Recordo bjec t.

URLs

URLs may no w be used in plac e o f c o nnec tio n strings ( see the Connectiono bjec t) and c o mmand text ( see the Commando 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 Streamo 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 Streamand Recordseto 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 general-purpo 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 t-o riented way ft-o r st-o met-o ne tt-o ac c ess a database sc hema. The sc hema prt-o 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 Catalogis the ro o t o bjec t. All o ther o bjec ts c an be referenc ed fro m the

Catalog. Beneath the Catalogo bjec t are the five main c o llec tio ns o f database o bjec ts, the Tablesc o llec tio n, the Groupsc o llec tio n, the Usersc o llec tio n, the

Proceduresc o llec tio n, and the Viewsc o llec tio n.

The Tablesc o llec tio n do c uments the physic al struc ture o f the data. Fro m the

Tableo b jec t, yo u c an find o ut the details o f the struc ture b y examining the

Columns, Indexes,and Keysc o llec tio ns. No te that the Indexand Keyo b jec ts also referenc e the Columnsc o llec tio n. Ho wever, b efo re yo u c an add a Column

(5)

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 Columndefinitions of a table, index, or key.

Continued

Tables Catalog

Table

Columns Column

Indexes Index

Keys Key

Columns Column

Columns Column Groups Group

Users User

Groups Group

Procedures Procedure

View s View

(6)

Table 11-2

(continued)

Object Description

Group Contains inform ation about a security group.

Groups Contains all of the security Groupobjects 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 Keyobjects 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 Viewobjects in the Catalog.

The Groupsc o llec tio n c o ntains the set o f sec urity gro ups. Eac h Groupo bjec t c o n-tains a referenc e to the c o llec tio n o f Usersthat are in that partic ular gro up. Befo re yo u c an add a user to the Usersc o llec tio n, it must already exist in the Usersgro up that is direc tly beneath the Catalogo bjec t.

The Usersc o llec tio n is similar to the Groupsc o llec tio n, exc ept that the users asso c iated with the datab ase are sto red in the Usersc o llec tio n, while the gro ups asso c iated with a single Userare sto red in the Groupsc o llec tio n. As yo u might expec t, in o rder to add a gro up to the Groupsc o llec tio n, yo u must first define it in the Groupso b jec t that is direc tly b eneath the Catalogo b jec t.

(7)

The Viewsc o llec tio n do c uments the set o f views in the database. Eac h view is defined in the Viewo bjec t. Like the Procedureo 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 pass-wo 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 Connectiono 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 Connectiono bjec t, either explic itly o r implic itly.

In additio n to pro viding a gateway to a data so urc e, the Connectiono 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 Errorsc o llec -tio n. The Errorsc 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 Errorsc 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 Errorsc o llec tio n. Thus, the Errorsc 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

Errorsc o llec tio n after handling the erro r o r immediately b efo re perfo rming a c ritic al o peratio n.

The Connectionobject 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 Commando bjec t, using the CommandTextand CommandTypepro perties. There are three basic types o f c o m-mands: SQL Statements, table names, and sto red pro c edures.

(8)

Onc e a Commandis defined and yo u have an o pen Connectionto a data so urc e, yo u may exec ute the Command. The c o mmand may use a set o f Parametersto c o ntro l ho w it wo rks. This info rmatio n is kept in the Parametersc o llec tio n. Parametersc 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 Commandmay also return a Recordseto bjec t, c o ntaining a set o f ro ws that were retrieved fro m the data so urc e.

A Recordsetwill always be returned if yo u exec ute an SQL Se le ctStatement 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

Recordsetdepending o n ho w the sto red pro c edure was written.

The Commandobject is discussed in detail in Chapter 13.

Playing with Recordsets

A Recordseto bjec t is the way yo u ac c ess the ac tual data sto red in yo ur database. The Recordsetmaintains 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 MoveNextand MovePreviousmetho ds. Yo u c an jump to the first ro w o r to the last ro w with the MoveFirstand MoveListmetho ds. Yo u c an save the c urrent rec o rd po inter and later resto re it using the Bookmarkpro perty.

Ac c ess to the info rmatio n in a partic ular ro w is thro ugh the Fieldsc o llec tio n. Eac h individual c o lumn in the Recordsetis represented by a Fieldo bjec t. Yo u c an ac c ess the c urrent value o f the c o lumn tho ugh the Valuepro perty. If the Recordsetwas 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 Valuepro perty. Yo u c an then save the c hanges to yo ur database by using the Updatemetho 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 Findmetho d , o r yo u c an filter the ro ws c urrently available in the

Recordsetto eliminate unwanted ro ws with the Filterpro perty. Yo u c an also refresh the data in the Recordsetby using the Requeryo r Resyncmetho d.

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

(9)

Summary

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

✦Yo u c an use the ADO Connectiono bjec t to ac c ess a database.

✦Yo u c an use the ADO Commando bjec t to run SQL statements and sto red pro c e-dures o n a database server.

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

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 eas-ier 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.

(10)

Gambar

Figure 11-1: Presenting the ADO object model
Table 11-1ADO Objects
Table 11-2ADOX Objects
Table 11-2 (continued)

Referensi

Dokumen terkait

Adapun tujuan diadakannya indeks saham syariah sebagaiman Jakarta Islamic Index yang melibatkan 30 saham terpilih, yaitu sebagai tolak ukur untuk mengukur kinerja

You can download and install the soft file of this incredible book Time Mends (Timber Wolves, Book 2) By Tammy Blackwell currently and in the link offered.. Yeah, different with

Indonesia merupakan negara dengan orang muslim terbesar didunia, akan tetapi setiap ormas muslim sendiri tentunya berbeda dalam memutuskan suatu masalah baru yang muncul

54 Tahun 2010 tentang Pengadaan Barang/Jasa Pemerintah serta menindaklanjuti proses seleksi untuk Paket Pekerjaan Pengadaan Meubelair (985 Unit) , bersama ini kami

n salinannya sebagaimana tertuang dalam lam proses pembuktian kualifikasi

Uang merupakan uang milik masyarakat atau uang beredar di masyarakat (di luar Bank Sentral seperti Bank Indonesia dan perbankan atau semua bank), yang terdiri dari :.. Uang Kertas

Puji syukur kami panjatkan kepada Tuhan Yang Maha Esa atas limpahan rahmat dan hidayah-Nya sehingga Prosiding Seminar Nasional MIPA Universitas Negeri Yogyakarta

As everybody recognizes, book Carr: Five Years Of Rape And Murder By Edna Buchanan is very popular as the window to open up the globe. It implies that reading publication Carr: