ADO Object Model

[Previous] [Next]

The ADO object model can be separated into five parts, with each part focused on a different object: the Connection object, the Command object, the Recordset object, the Record object, and the Stream object. We'll cover the object model, focusing on these five objects, in more depth in Chapters 3 through 6. Here's a brief overview of the three core objects: Connection, Command, and Recordset.

Connection Object

As its name implies, the Connection object is your connection to the database; it manages the communication between your application and your database. (Both the Recordset and Command objects have an ActiveConnection property that you can use to refer to a Connection object.) Use the Open and Close methods of the Connection object to open and close the connection to your database.

The Connection object is at the top of the less-than-stringent hierarchy of ADO objects. It is comparable to the DAO Database object and the RDO rdoConnection object. ADO has no equivalent of DAO's DBEngine and Workspace objects or RDO's rdoEngine and rdoEnvironment objects. Unlike the DAO and RDO object models, ADO has no collection object in its object model containing all of the Connection objects.

Through its transactional methods—BeginTrans, CommitTrans, and RollbackTrans—the Connection object allows you to manage the changes you make to your database in transactions. The OpenSchema method lets you view much of the metadata about your database. If you're using ADO's asynchronous features, events on the Connection object will fire to signify the completion of the asynchronous call. To determine why your query failed, you can evaluate the Errors collection of the Connection object.

Use the Connection object's Execute method to submit queries, including queries that generate Recordsets. The Connection object's Execute method also allows you to issue an action query to modify data, manipulate objects, or change database-specific settings on your database.

Command Object

The ADO Command object is designed to help you work with queries that you'll execute repeatedly or queries that will check the value of an output or return parameter on a call to a stored procedure. The Command object is similar to DAO's QueryDef object and RDO's rdoQuery object.

You can submit the query in your Command object to your database via the Command object's Execute method. As with the Connection object, you can issue an action query or a query that will return a Recordset. The ActiveCommand property on the Recordset object refers back to the Command object used to open that Recordset. Use the Command object's Parameters collection to work with parameterized queries and stored procedures. The ActiveConnection property on the Command object refers back to the Connection object.

Asynchronous Function Calls

Asynchronous calls pose an added challenge for programmers. Usually when you make an asynchronous call, there's code that you want to execute as soon as the call is completed. Events that signify the completion of an asynchronous call can greatly simplify your programming. Without these events, you need to use another thread to perennially check to determine when the call has completed. Programmers using ADO 1.5 could submit queries asynchronously but had to check the value of the Recordset object's State property to determine if the query had completed. Such code is suboptimal at best. ADO 2.0 simplified asynchronous programming for developers by firing events when asynchronous calls completed.

Recordset Object

Here's where you get your data. The Recordset object contains the results of your query. These results consist of rows (called records) and columns (called fields) of data. Each column is stored in a Field object in the Recordset's Fields collection. The Recordset object is the equivalent of DAO's Recordset object and RDO's rdoResultset object.

You can use the Open method of the Recordset object to retrieve the results of a query either through the Command object or directly through the Connection object. You can also generate a Recordset object by calling the Execute method on either the Connection or Command object. Flexible, but confusing. The Recordset's ActiveConnection property refers to the Connection object that the Recordset uses to communicate with the database. Similarly, the ActiveCommand property refers to the Command object that generated the Recordset.



Programming ADO
Programming MicrosoftВ® ADO.NET 2.0 Core Reference
ISBN: B002ECEFQM
EAN: N/A
Year: 2000
Pages: 131
Authors: David Sceppa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net