The Connection Object - The RollbackTransComplete Event
(Page 14 of 15 )
This event fires after a RollbackTrans method call has finished executing.
RollbackTransComplete(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 | The Connection object upon which the RollbackTrans method was called |
You can use this event to trigger other operations that depend upon the transaction having failed, such as writing to log files. For example:
Private Sub objConn_RollbackTransComplete( _
ByVal pError As ADODB.Error, _
adStatus As ADODB.EventStatusEnum, _
ByVal pConnection As ADODB.Connection)
Print "Transaction was rolled back. " & _
"Changes have not been saved."
End Sub
This could be extremely useful in nightly batch jobs.
The WillConnect Event
This event fires before a connection is opened, indicating that the connection is about to be established.
WillConnect(ConnectionString, UserID, Password, _
Options, adStatus, pConnection)
| Parameter | Type | Description |
|---|
| Connection | String | Connection information |
| String | | |
| UserID | String | User name to use when connecting |
| Password | String | User password to use when connecting |
| Options | Long | Extra connection options, as passed into the |
| | Options parameter of the Connection object’s |
| | Open method |
| adStatus | EventStatus Enum (Long) | Identifies the status of the event |
| | |
| pConnection | Connection | Connection object for which this event applies |
The parameters supplied can be changed before the method returns-for instance if the user has specified certain connection attributes, but you wish to change them. As an example, imagine an application that allowed the user to specify connection details. You could prevent them from connecting as a certain user, but allow the connection to be established as another:
Private Sub objConn_WillConnect(ConnectionString As _
String, UserID As String, Password As String, _
Options As Long, _
adStatus As ADODB.EventStatusEnum, _
ByVal pConnection As ADODB.Connection)
If adStatus = adStatusOK Then
Select Case UserID
Case "sa"
Print "Connection as system " & _
"administrator not allowed."
adStatus = adStatusCancel
Case "Guest"
UserID = "GuestUser"
Password = "GuestPassword"
End Select
End If
End Sub
This stops the user trying to connect as sa and cancels the connection attempt. If a user tries to connect as Guest, then the user ID is changed to GuestUser and the connection proceeds. This allows you to have a set of real user details that are hidden, while exposing a viewable set of user details.
The WillExecute Event
This event fires before a pending command executes on the connection.
WillExecute(Source, CursorType, LockType, _
Options, adStatus, pCommand, _
pRecordset, pConnection)
| Name | Type | Description |
|---|
| Source | String | The SQL command or stored procedure name |
| CursorType | CursorTypeEnum(Long) | Type of cursor for the recordset that will be opened (if adOpenUnspecified, cursor type cannot change) |
| | |
| | |
| LockType | LockTypeEnum(Long) | Lock type for the recordset that will be opened (if adLockUnspecified, lock type cannot be changed) |
| | |
| | |
| Options | Long | Options that can be used to execute the command or open the recordset, as passed into the Options argument |
| | |
| | |
| adStatus | EventStatusEnum(Long) | Identifies the status of the event |
| | |
| pCommand | Command | Command object for which this event applies (may be empty if a Command object was not being used) |
| | |
| | |
| pRecordset | Recordset | Recordset object for which this event applies |
| | (will be empty Recordset object if no recordset returned by Execute method) |
| | |
| pConnection | Connection | Connection object for which this event applies |
The execution parameters can be modified in this procedure, because it is called before the command executes.
This is particularly useful when building user-query type applications where the user has the ability to set details of the connection, because it allows you to examine the parameters and modify them if necessary. For example:
Private Sub objConn_WillExecute(Source As String, _
CursorType As ADODB.CursorTypeEnum, _
LockType As ADODB.LockTypeEnum, _
Options As Long, adStatus As ADODB.EventStatusEnum, _
ByVal pCommand As ADODB.Command, _
ByVal pRecordset As ADODB.Recordset, _
ByVal pConnection As ADODB.Connection)
If Source = "SalaryDetails" Then
Print " Nice try, but you're not " & _
"allowed to look at these"
adStatus = adStatusCancel
End If
End Sub
The preceding code cancels the event if someone tries to connect to the SalaryDetails table.
You can also use this technique to protect against ad hoc insertions and deletions, and it is an easy way to build business logic into the connection. A better way to protect against this sort of amendment to data or tables is to implement proper security and to use a three-tier business model, where data access is only possible through controlled operations.
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: Collections of the Connection Object >>
More ASP.NET Articles
More By Apress Publishing