The Connection Object - The Disconnect Event
(Page 13 of 15 )
This event fires after a connection is closed.
Disconnect(adStatus, pConnection)
| Parameter | Type | Description |
|---|
| adStatus | EventStatusEnum (Long) | Identifies the status of the event |
| pConnection | Connection | The Connection object for which this event applies You can use this to examine whether the disconnection was successful. |
You can also use it to track users as they log on to and off from data sources. It can also be useful to alert users when a connection drops unexpectedly.
The ExecuteComplete Event
This event fires after a statement has finished executing.
ExecuteComplete(RecordsAffected, pError, _
adStatus, pCommand, _
pRecordset, pConnection)
| Parameter | Type | Description |
|---|
| Records Affected | Long | A Long variable into which the provider returns the number of records that the operation affected |
| pError | Error | An Error object that describes the error that occurred if adStatus is adStatusErrorsOccurred (not set otherwise) |
| adStatus | EventStatus Enum (Long) | Identifies the status of the event |
| pCommand | Command | Command object for which this event applies (may not be set if a Command object was not used) |
| pRecordset | Recordset | Recordset object upon which the Execute was run (may be empty if a non-recordset-returning command was run, such as an action query) |
| pConnection | Connection | Connection object upon which the Execute method was called |
This allows you to examine whether the command completed successfully and how many records it affected. You can use this instead of the Execute method’s RecordsAffected argument. The following Visual Basic code shows the use of this event:
Private Sub objConn_ExecuteComplete( _
ByVal RecordsAffected As_Long, _
ByVal pError As ADODB.Error, _
ByVal adStatus As ADODB.EventStatusEnum, _
ByVal pCommand As ADODB.Command, _
ByVal pRecordset As ADODB.Recordset, _
ByVal pConnection As ADODB.Connection)
If adStatus = adStatusOK Then Print RecordsAffected & _ " records were affected by this command." End If |
End Sub
The nonhighlighted code is the Visual Basic generated event procedure.
The InfoMessage Event
This event fires whenever a connection event operation completes successfully and the provider returns additional information, such as a warning.
InfoMessage(pError, adStatus, pConnection)
| Parameter | Type | Description |
|---|
| pError | Error | An Error object that describes the error that occurred if adStatus is adStatusErrorsOccurred (not set otherwise) |
| adStatus | EventStatusEnum (Long) | Identifies the status of the event |
| pConnection | Connection | Connection object upon which the statement was executed |
The parameters define what type of information message this is. This is particularly useful when dealing with ODBC data sources, especially to SQL Server, because it returns informational messages that could be logged in an audit trail.
For example, you could connect to the pubs database on SQL server with this connect string:
Driver={SQL Server}; Server=Tigger; Database=pubs; _
UID=sa; PWD=
and then put this code into the InfoMessage event procedure:
Private Sub oConn_InfoMessage(ByVal pError As _
ADODB.Error, _
adStatus As ADODB.EventStatusEnum, _
ByVal pConnection As ADODB.Connection)
Dim objError As ADODB.Error
Debug.Print pError.Description
For Each objError In pConnection.Errors
Debug.Print vbtab; objError.Description
Next
End Sub
On my server, the above code generates the following warnings when connecting to the pubs database in SQL Server using the OLE DB Provider for ODBC:
[Microsoft][ODBC SQL Server Driver]
[SQL Server]Changed database context to 'master'.
[Microsoft][ODBC SQL Server Driver]
[SQL Server]Changed database context to 'master'.
[Microsoft][ODBC SQL Server Driver]
[SQL Server]Changed language setting to 'us_english'.
[Microsoft][ODBC SQL Server Driver]
[SQL Server]Changed database context to 'pubs'.
In general, this event can be used to track connection messages or actions on the connection that return the ODBC SQL_SUCCESS_WITH_INFO result.
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 RollbackTransComplete Event >>
More ASP.NET Articles
More By Apress Publishing