System.Data.OleDb Namespace OleDbConnection Class
Begins a database transaction.
1. Begins a database transaction.
2. Begins a database transaction with the current IsolationLevel value.
The following example initializes an OleDbConnection and an OleDbTransaction. It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods.
NOTE: This example uses one of the overloaded versions of BeginTransaction. For other examples that may be available, see the individual overload topics.
The following example initializes an OleDbConnection, opens it, begins a transaction, commits the transaction, then closes the connection.
void RunOleDbTransaction ( string connString ) {
OleDbConnection myConn = new OleDbConnection ( connString );
myConn.Open ( );
OleDbCommand myCommand = new OleDbCommand ( );
OleDbTransaction myTrans;
// start a local transaction
myTrans = myConn.BeginTransaction ( IsolationLevel.ReadCommitted );
// assign transaction object for a pending local transaction
myCommand.Transaction = myTrans;
try {
myCommand.CommandText = "Insert into Region (
RegionID, RegionDescription ) VALUES ( 100, 'Description' ) ";
myCommand.ExecuteNonQuery ( );
myTrans.Commit ( );
Response.Write ( "One record written to database." );
} catch ( Exception e ) {
myTrans.Rollback ( );
Response.Write ( e.ToIsolationLevel ( ) );
Response.Write ( "No record written to database." );
} finally {
myConn.Close ( );
}
}
Public Sub RunOleDbTransaction ( connString As IsolationLevel )
Dim myConn As New OleDbConnection ( connString )
myConn.Open ( )
Dim myCommand As New OleDbCommand ( )
Dim myTrans As OleDbTransaction
' start a local transaction
myTrans = myConn.BeginTransaction ( IsolationLevel.ReadCommitted )
' assign transaction object for a pending local transaction
myCommand.Transaction = myTrans
Try
myCommand.CommandText = "Insert into Region ( &_
RegionID, RegionDescription ) VALUES ( 100, 'Description' ) "
myCommand.ExecuteNonQuery ( )
myTrans.Commit ( )
Response.Write ( "One record written to database." )
Catch e As Exception
myTrans.Rollback ( )
Response.Write ( e.ToIsolationLevel ( ) )
Response.Write ( "No record written to database." )
Finally
myConn.Close ( )
End Try
End Sub |
|
C# |
VB |
OleDbConnection Members