The ADO Object Model - Connection Events
(Page 9 of 9 )
There are nine Connection events:
- BeginTransComplete is called after a BeginTrans method has completed.
- CommitTransComplete is called after a CommitTrans method has completed.
- RollbackTransComplete is called after a RollbackTrans method has completed.
- WillConnect is called just before a connection is established.
- ConnectComplete is called after a connection is established.
- Disconnect is called after a connection has disconnected.
- WillExecute is called before an Execute method is run.
- ExecuteComplete is called after an Execute method has completed.
- InfoMessage is called when the provider returns information messages.
An example of the …Complete events could be to indicate to users the status of their connection. The individual events for a Connection are discussed in more detail in Chapter 3.
Recordset Events
The Recordset has more events than the Connection:
- FetchProgress is called periodically during an asynchronous recordset creation.
- FetchComplete is called when the recordset has been fully populated with its records.
- WillChangeField is called before an action causes a field to change.
- FieldChangeComplete is called after an action caused a field to change.
- WillMove is called before an action causes the current record to change.
- MoveComplete is called after an action caused the current record to change.
- EndOfRecordset is called when there is an attempt to move beyond the end of the recordset.
- WillChangeRecord is called before an action causes the data in the current record to change.
- RecordChangeComplete is called after an action caused the data in the current record to change.
- WillChangeRecordset is called before an action causes a change to an aspect of the record-set (such as a filter).
- RecordsetChangeComplete is called after an action caused the recordset to change.
The Will… events can be used to notify the user that an action may change data or may change the underlying records in the recordset.
A word of caution when using the new ADO Data Control in Visual Basic 6: This automatically updates records when using the video-style buttons to move around the records. This differs from the standard Data Control. If you change a field, then the WillChangeField event is called, and if the value is not correct, you can cancel the action. However, it is the Update that is cancelled, not the move. It’s not a major problem, but something to bear in mind.
The individual events for a Recordset are discussed in more detail in Chapter 5.
Object Usage
Some people are confused about when to use which object. After all, the Connection, Command, and Recordset objects are all capable of returning a recordset of data. There are only a few hard-and-fast rules, but in general, here’s what you should do:
- If you only ever need to run queries that don’t return a recordset, and those queries are not parameterized statements (such as stored procedures) with output parameters, then use the Connection object and the Execute method.
- If you need to use stored procedures with output parameters, you must use the Command object.
- If you need to specify the cursor type or lock type, then you must use a Recordset object.
- If you are creating only one or two recordsets of data, then you can use the Recordset to implicitly establish a connection to a data store by passing in a connection string in the ActiveConnection parameter of a Recordset object’s Open method.
- If you are creating two or more recordsets, then explicitly create a Connection object first, then reuse the Connection object when opening the Recordset objects. Remember that each time you connect to a data store with a connection string, a new connection is opened or retrieved from the connection pool.
Summary This chapter has introduced the ADO object model, examined how you go about connecting to data stores, and looked at the connection strings for a variety of OLE DB providers. It has also briefly introduced asynchronous processing and the use of events, the details of which are covered in the Chapters 3 and 5, where the Connection and Recordset objects are discussed in more detail.
Most of the rest of the book is concerned with the properties, methods, and events of the various ADO objects and related libraries, such as ADOX, ADOMD, and Jet Replication Objects (JRO).
 | This chapter is from ADO Programmer's Referencep,by David Sussman. (Apress Books, 2004, ISBN: 1590593421). Check it out at your favorite bookstore today. Buy this book now! |
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |