asp.net.ph

OleDbConnection.BeginTransaction Method

System.Data.OleDb Namespace   OleDbConnection Class


Begins a database transaction.

Overload List

1. Begins a database transaction.

2. Begins a database transaction with the current IsolationLevel value.


Example

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 ( );
   }
}
  C# VB

See Also

OleDbConnection Members Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph