TransactionStateChangeEventArgs Class
This sample demonstrates how the common base TransactionStateChangeEventArgs class for TransactionStateChanged and TransactionStateChanging envent arguments allows using more general code.
SqlConnection connection = new SqlConnection(
"User Id=sa;Server=localhost;Initial Catalog=Test;");
connection.TransactionStateChanged += new TransactionStateChangedEventHandler(connection_TransactionStateChange);
connection.TransactionStateChanging += new TransactionStateChangingEventHandler(connection_TransactionStateChange);
connection.Open();
// ...
SqlTransaction transaction = connection.BeginTransaction();
// ...
transaction.Commit();
// ...
void connection_TransactionStateChange(object sender, TransactionStateChangeEventArgs e) {
// common actions that must be done before and after the transaction state change
// ...
if (e is TransactionStateChangingEventArgs) {
// actions before the transaction state change
// ...
}
else { // e is TransactionStateChangedEventArgs
// actions after the transaction state change
// ...
}
}
Dim connection As New SqlConnection( _
"User Id=sa;Server=localhost;Initial Catalog=Test;")
AddHandler connection.TransactionStateChanged, New TransactionStateChangedEventHandler(AddressOf &_ Me.connection_TransactionStateChange)
AddHandler connection.TransactionStateChanging, New TransactionStateChangingEventHandler(AddressOf &_ Me.connection_TransactionStateChange)
connection.Open()
' ...
Dim Transaction As OracleTransaction
Transaction = connection.BeginTransaction()
' ...
Transaction.Commit()
' ...
Private Sub connection_TransactionStateChange(ByVal sender As Object, ByVal e As TransactionStateChangeEventArgs)
' common actions that must be done before and after the transaction state change
'...
If TypeOf e Is TransactionStateChangingEventArgs Then
' actions before the transaction state change
' ...
Else ' e is TransactionStateChangedEventArgs
' actions after the transaction state change
' ...
End If
End Sub
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2