asp.net.ph

SqlConnection.BeginTransaction Method

System.Data.SqlClient Namespace   SqlConnection Class


Begins a database transaction.

Overload List

1. Begins a database transaction.

2. Begins a database transaction with the specified isolation level.

3. Begins a database transaction with the specified transaction name.

4. Begins a database transaction with the specified isolation level and transaction name.


Example

The following example initializes an SqlConnection and an SqlTransaction. 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.

void RunSqlTransaction ( string connString ) {
   SqlConnection myConn = new SqlConnection ( connString );
   myConn.Open ( );

   SqlCommand myCommand = new SqlCommand ( );
   SqlTransaction myTrans;

   // start a local transaction
   myTrans = myConn.BeginTransaction ( IsolationLevel.ReadCommitted, "sampleTrans" );
   // 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

SqlConnection Members   SqlTransaction 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