IDbTransaction Interface
Represents a transaction to be performed at a data source, and is implemented by .NET Framework data providers that access relational databases.
For a list of all members of this type, see IDbTransaction Members.
System.IDisposable
System.Data.IDbTransaction
[Visual Basic] Public Interface IDbTransaction Inherits IDisposable [C#] public interface IDbTransaction : IDisposable [C++] public __gc __interface IDbTransaction : public IDisposable [JScript] public interface IDbTransaction implements IDisposable
Classes that Implement IDbTransaction
| Class | Description |
|---|---|
| OdbcTransaction | Represents an SQL transaction to be made at a data source. This class cannot be inherited. |
| OleDbTransaction | Represents an SQL transaction to be made at a data source. This class cannot be inherited. |
| OracleTransaction | Represents a transaction to be made at a database. This class cannot be inherited. |
| SqlCeTransaction | Represents an SQL transaction to be made at a data source. This class cannot be inherited. |
| SqlTransaction | Represents a Transact-SQL transaction to be made in a 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 Framework data providers, see Implementing a .NET Framework Data Provider.
An application does not create an instance of the IDbTransaction interface directly, but creates 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 Framework 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 Framework data provider namespace. For example, Sql is the prefix of the SqlTransaction class in the System.Data.SqlClient namespace.
Example
[Visual Basic, C#, C++] The following example creates instances of the derived classes, SqlConnection and SqlTransaction. It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods.
[Visual Basic] Public Sub RunSqlTransaction(myConnString As String) Dim myConnection As New SqlConnection(myConnString) myConnection.Open() Dim myCommand As SqlCommand = myConnection.CreateCommand() Dim myTrans As SqlTransaction ' Start a local transaction myTrans = myConnection.BeginTransaction() ' Must assign both transaction object and connection ' to Command object for a pending local transaction myCommand.Connection = myConnection myCommand.Transaction = myTrans Try myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')" myCommand.ExecuteNonQuery() myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')" myCommand.ExecuteNonQuery() myTrans.Commit() Console.WriteLine("Both records are written to database.") Catch e As Exception Try myTrans.Rollback() Catch ex As SqlException If Not myTrans.Connection Is Nothing Then Console.WriteLine("An exception of type " & ex.GetType().ToString() & _ " was encountered while attempting to roll back the transaction.") End If End Try Console.WriteLine("An exception of type " & e.GetType().ToString() & _ "was encountered while inserting the data.") Console.WriteLine("Neither record was written to database.") Finally myConnection.Close() End Try End Sub 'RunSqlTransaction [C#] public void RunSqlTransaction(string myConnString) { SqlConnection myConnection = new SqlConnection(myConnString); myConnection.Open(); SqlCommand myCommand = myConnection.CreateCommand(); SqlTransaction myTrans; // Start a local transaction myTrans = myConnection.BeginTransaction(); // Must assign both transaction object and connection // to Command object for a pending local transaction myCommand.Connection = myConnection; myCommand.Transaction = myTrans; try { myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"; myCommand.ExecuteNonQuery(); myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"; myCommand.ExecuteNonQuery(); myTrans.Commit(); Console.WriteLine("Both records are written to database."); } catch(Exception e) { try { myTrans.Rollback(); } catch (SqlException ex) { if (myTrans.Connection != null) { Console.WriteLine("An exception of type " + ex.GetType() + " was encountered while attempting to roll back the transaction."); } } Console.WriteLine("An exception of type " + e.GetType() + " was encountered while inserting the data."); Console.WriteLine("Neither record was written to database."); } finally { myConnection.Close(); } } [C++] public: void RunSqlTransaction(String* myConnString) { SqlConnection* myConnection = new SqlConnection(myConnString); myConnection->Open(); SqlCommand* myCommand = myConnection->CreateCommand(); SqlTransaction* myTrans; // Start a local transaction myTrans = myConnection->BeginTransaction(); // Must assign both transaction object and connection // to Command object for a pending local transaction myCommand->Connection = myConnection; myCommand->Transaction = myTrans; try { myCommand->CommandText = S"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"; myCommand->ExecuteNonQuery(); myCommand->CommandText = S"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"; myCommand->ExecuteNonQuery(); myTrans->Commit(); Console::WriteLine(S"Both records are written to database."); } catch(Exception* e) { try { myTrans->Rollback(); } catch (SqlException* ex) { if (myTrans->Connection != 0) { Console::WriteLine(S"An exception of type {0} was encountered while attempting to roll back the transaction.", ex->GetType()); } } Console::WriteLine(S"An exception of type {0} was encountered while inserting the data.", e->GetType()); Console::WriteLine(S"Neither record was written to database."); } __finally { myConnection->Close(); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Data
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
Assembly: System.Data (in System.Data.dll)