The Connection Object - The Cancel Method
(Page 3 of 15 )
This method cancels the execution of a pending, asynchronous Execute or Open operation.
Connection.Cancel
This is particularly useful when writing applications that allow a user to specify connection details, or when users explicitly log on to a connection. If, after a certain delay, the connection has not been established, you can inform the user and offer the option of canceling the connection attempt. For example, in Visual Basic you could open the connection asynchronously and offer the user a Cancel button, which would call this method.
The Close Method This method closes an open connection and any dependent objects.
Connection.Close
Closing a connection does not remove it from memory, and you can change its settings and reopen it. To free the object from memory (assuming no one else is using the object), you must set the object variable to Nothing. For example:
objConn.Close
Set objConn = Nothing
To avoid getting an error when trying to close a connection that is not open, you can check the Connection object’s State property:
If objConn.State = adStateOpen Then
objConn.Close
End If
Set objConn = Nothing
When the connection’s Close method is called, associated recordsets and commands behave differently. Any Recordset objects associated with the connection are closed. Any Command objects associated with the connection will persist, but the ActiveConnection parameter will be cleared, thus disassociating the command from any connection.
When the Close method is called, any pending changes in Recordset objects associated with the connection will be cancelled. If you call the Close method while a transaction is in progress, an error will be generated. This is a run-time error, number 3246, saying that the Connection object cannot be explicitly closed while in a transaction. In comparison, if a Connection object falls out of scope, any incomplete transactions will be rolled back, and in this case no error is generated.
The CommitTrans Method
This method saves any pending changes and ends the current transaction.
Connection.CommitTrans
All changes made since the previous BeginTrans will be written to the data store. This only affects the most recently opened transaction, and you must resolve lower-level transactions before resolving higher-level ones. So, if you are nesting transactions, you cannot start two transactions and then Commit or Abort the outer transaction without first Committing or Aborting the inner transaction, because they refer to the same connection.
If the Connection object’s Attributes property is set to adXactCommitRetaining, the provider automatically starts a new transaction after a CommitTrans call.
See also the BeginTrans method and the RollbackTrans method.
This is from ADO Programmer's Reference, by Dave Sussman (Apress, ISBN 1590593421). Check it out at your favorite bookstore today. Buy this book now. |
Next: The Execute Method >>
More ASP.NET Articles
More By Apress Publishing