Transactions in ADO.NET are used when you want to bind multiple tasks together so that they execute as a single unit of work. For example, imagine that an application performs two tasks. First, it updates a table with order information. Second, it updates a table that contains inventory information, debiting the items ordered. If either task fails, the both updates are rolled back.
A transaction considered to be a local transaction when it is a single-phase transaction and is handled by the database directly. Transactions are considered to be distributed transactions when they are coordinated by a transaction monitor and use fail-safe mechanisms (such as two-phase commit) for transaction resolution.
Each of the .NET Framework data providers has its own
Transaction object for performing local transactions. If you require a transaction to be performed in a SQL Server database, select a System.Data.SqlClient transaction. For an Oracle transaction, use the System.Data.OracleClient provider. In addition, there is a new DbTransaction class that is available for writing provider-independent code that requires transactions.
Transactions are most efficient when it is performed on the server. If you are working with a SQL Server database that makes extensive use of explicit transactions, consider writing them as stored procedures using the Transact-SQL BEGIN TRANSACTION statement. For more information about performing server-side transactions, see SQL Server Books Online.
In ADO.NET, you control transactions with the
Connection object. You can initiate a local transaction with the
BeginTransaction method. Once you have begun a transaction, you can enlist a command in that transaction with the
Transaction property of a
Command object. You can then commit or roll back modifications made at the data source based on the success or failure of the components of the transaction.
The scope of the transaction is limited to the connection. The following example performs an explicit transaction that consists of two separate commands in the
try block. The commands execute INSERT statements against the Production.ScrapReason table in the AdventureWorks SQL Server sample database, which are committed if no exceptions are thrown. The code in the
catch block rolls back the transaction if an exception is thrown. If the transaction is aborted or the connection is closed before the transaction has completed, it is automatically rolled back.
Follow these steps to perform a transaction.
Call the BeginTransaction method of the SqlConnection object to mark the start of the transaction. The BeginTransaction method returns a reference to the transaction. This reference is assigned to the SqlCommand objects that are enlisted in the transaction.
Transactionobject to the Transaction property of the SqlCommand to be executed. If a command is executed on a connection with an active transaction, and the
Transactionobject has not been assigned to the
Transactionproperty of the
Commandobject, an exception is thrown.
Execute the required commands.
Call the Commit method of the SqlTransaction object to complete the transaction, or call the Rollback method to end the transaction. If the connection is closed or disposed before either the Commit or Rollback methods have been executed, the transaction is rolled back.
The following code example demonstrates transactional logic using ADO.NET with Microsoft SQL Server.
[!CODE [DataWorks SqlTransaction.Local#1](../CodeSnippet/VS_Snippets_ADO.NET/DataWorks SqlTransaction.Local#1)]