asp.net.ph

IDbTransaction Interface

System.Data Namespace


Represents a transaction to be performed at a data source, and is implemented by .NET data providers that access relational databases.

IDbTransaction Interface Members

Collapse   Properties

Visibility Name Value Type Accessibility
public Connection IDbConnection [ Get ]
public IsolationLevel IsolationLevel [ Get ]

Collapse   Methods

Visibility Name Parameters Return Type
public Commit ( ) Void
public Rollback ( ) Void

Classes that Implement IDbTransaction


Class Description
OleDbTransaction Represents an SQL transaction to be made at a data source.
SqlTransaction Represents a Transact-SQL transaction to be made in an SQL Server™ database. This class cannot be inherited.

Remarks

The IDbTransaction interface allows an inheriting class to implement a Transaction class, which represents the transaction to be performed at a data source. For more information about Transaction classes, see Performing Transactions. For more information about implementing .NET data providers, see Implementing a .NET Data Provider.

An application does not create an instance of the IDbTransaction interface directly, but initializes an instance of a class that inherits IDbTransaction.

Classes that inherit IDbTransaction must implement the inherited members, and typically define additional members to add provider-specific functionality. For example, the IDbTransaction interface defines the Commit method. In turn, the OleDbTransaction class inherits this property, and also defines the Begin method.

Notes to Implementers: To promote consistency among .NET data providers, name the inheriting class in the form Prv Transaction where Prv is the uniform prefix given to all classes in a specific .NET data provider namespace. For example, Sql is the prefix of the SqlTransaction class in the System.Data.SqlClient namespace.

Example

The following example initializes instances of the derived classes, SqlConnection and SqlTransaction. It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods.

void RunSqlTransaction ( string connString ) {
   SqlConnection myConn = new SqlConnection ( connString );
   myConn.Open ( );
   SqlCommand myCmd = new SqlCommand ( );
   SqlTransaction myTrans;


   // start a local transaction
   myTrans = myConn.BeginTransaction ( );

   // assign transaction object for a pending local transaction
   myCmd.Transaction = myTrans;
   try {
      myCmd.CommandText = "Insert into Region 
         ( RegionID, RegionDescription ) VALUES ( 100, 'Description' ) ";
      myCmd.ExecuteNonQuery ( );
      myCmd.CommandText = "Insert into Region 
         ( RegionID, RegionDescription ) VALUES ( 101, 'Description' ) ";
      myCmd.ExecuteNonQuery ( );
      myTrans.Commit ( );
      Response.Write ( "Both records are written to database." );
  }
   catch ( Exception e ) {
      myTrans.Rollback ( );
      Response.Write ( e.ToString ( ) );
      Response.Write ( "Neither record was written to database." );
  }
   finally {
      myConn.Close ( );
   }
}
  C# VB

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