System.Data.OleDbClient Namespace OleDbConnection Class
Begins a database transaction.
[ VB ]
Overloads Overrides Public Function BeginTransaction ( ) As OleDbTransaction
[ C# ]
public override OleDbTransaction BeginTransaction ( );
[ C++ ]
public: OleDbTransaction BeginTransaction ( );
[ JScript ]
public override function BeginTransaction ( ) : OleDbTransaction
An object representing the new transaction.
You must explicity commit or roll back the transaction using the Commit or Rollback method. To ensure that the OLE DB .NET Data Provider transaction management model performs correctly, avoid using other transaction management models, such as those provided by the data source.
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 ( );
// 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.ToString ( ) );
Response.Write ( "No record written to database." );
} finally {
myConn.Close ( );
}
}
Public Sub RunOleDbTransaction ( connString As String )
Dim myConn As New OleDbConnection ( connString )
myConn.Open ( )
Dim myCommand As New OleDbCommand ( )
Dim myTrans As OleDbTransaction
' start a local transaction
myTrans = myConn.BeginTransaction ( )
' 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.ToString ( ) )
Response.Write ( "No record written to database." )
Finally
myConn.Close ( )
End Try
End Sub |
|
C# |
VB |
OleDbConnection Members OleDbConnection.BeginTransaction Overload List Commit Save Rollback