System.Data Namespace
Represents a transaction to be performed at a data source, and is implemented by .NET data providers that access relational databases.
Class |
Description |
OleDbTransaction |
Represents an SQL transaction to be made at a data source. |
SqlTransaction |
Represents a Transact-SQL transaction to be made in an SQL Server™ database. This class cannot be inherited. |
The IDbTransaction interface allows an inheriting class to implement a Transaction class, which represents the transaction to be performed at a data source. For more information about Transaction classes, see Performing Transactions. For more information about implementing .NET data providers, see Implementing a .NET Data Provider.
An application does not create an instance of the IDbTransaction interface directly, but initializes an instance of a class that inherits IDbTransaction.
Classes that inherit IDbTransaction must implement the inherited members, and typically define additional members to add provider-specific functionality. For example, the IDbTransaction interface defines the Commit method. In turn, the OleDbTransaction class inherits this property, and also defines the Begin method.
Notes to Implementers: To promote consistency among .NET data providers, name the inheriting class in the form Prv Transaction where Prv is the uniform prefix given to all classes in a specific .NET data provider namespace. For example, Sql is the prefix of the SqlTransaction class in the System.Data.SqlClient namespace.
The following example initializes instances of the derived classes, SqlConnection and SqlTransaction. It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods.
void RunSqlTransaction ( string connString ) {
SqlConnection myConn = new SqlConnection ( connString );
myConn.Open ( );
SqlCommand myCmd = new SqlCommand ( );
SqlTransaction myTrans;
// start a local transaction
myTrans = myConn.BeginTransaction ( );
// assign transaction object for a pending local transaction
myCmd.Transaction = myTrans;
try {
myCmd.CommandText = "Insert into Region
( RegionID, RegionDescription ) VALUES ( 100, 'Description' ) ";
myCmd.ExecuteNonQuery ( );
myCmd.CommandText = "Insert into Region
( RegionID, RegionDescription ) VALUES ( 101, 'Description' ) ";
myCmd.ExecuteNonQuery ( );
myTrans.Commit ( );
Response.Write ( "Both records are written to database." );
}
catch ( Exception e ) {
myTrans.Rollback ( );
Response.Write ( e.ToString ( ) );
Response.Write ( "Neither record was written to database." );
}
finally {
myConn.Close ( );
}
}
Public Sub RunSqlTransaction ( connString As String )
Dim myConn As New SqlConnection ( connString )
myConn.Open ( )
Dim myCmd As New SqlCommand ( )
Dim myTrans As SqlTransaction
' start a local transaction
myTrans = myConn.BeginTransaction ( )
' assign transaction object for a pending local transaction
myCmd.Transaction = myTrans
Try
myCmd.CommandText = "Insert into Region _
( RegionID, RegionDescription ) VALUES ( 100, 'Description' ) "
myCmd.ExecuteNonQuery ( )
myCmd.CommandText = "Insert into Region _
( RegionID, RegionDescription ) VALUES ( 101, 'Description' ) "
myCmd.ExecuteNonQuery ( )
myTrans.Commit ( )
Response.Write ( "Both records are written to database." )
Catch e As Exception
myTrans.Rollback ( )
Response.Write ( e.ToString ( ) )
Response.Write ( "Neither record was written to database." )
Finally
myConn.Close ( )
End Try
End Sub 'RunSqlTransaction |
|
C# |
VB |