asp.net.ph

IDbConnection.BeginTransaction Method ( IsolationLevel )

System.Data Namespace   IDbConnection Class


Begins a database transaction with the specified isolation level.

[ VB ]
Overloads Public Function BeginTransaction ( _
   ByVal iso As IsolationLevel _
) As IDbTransaction

[ C# ]
public IDbTransaction BeginTransaction (
   IsolationLevel iso
);

[ C++ ]
public: IDbTransaction BeginTransaction (
   IsolationLevel* iso
);

[ JScript ]
public function BeginTransaction (
   iso : IsolationLevel
) : IDbTransaction

Parameters

iso
The isolation level under which the transaction should run.

Return Value

An object representing the new transaction.

Exceptions


Exception Type Condition
InvalidOperationException Occurs when Parallel transactions are not supported.

Remarks

Once the transaction has completed, you must explicity commit or roll back the transaction using the Commit or Rollback method.

Example

The following example initializes an SqlConnection, opens it, begins a transaction, commits the transaction, then closes the connection.

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 );
   // 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

IDbConnection Members   IDbConnection.BeginTransaction Overload List   Commit   Save   Rollback 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