This documentation is archived and is not being maintained.

Performing Transactions

Transactions are a group of operations combined into a logical unit of work, and are used to control and maintain the consistency and integrity of each action in a transaction despite errors that might occur in the system.

For example, in a banking application where funds are transferred from one account to another, one account is credited an amount in a database table, and another account is debited the same amount at the same time in another database table. Because computers can fail due to power outages, network outages, and so on, it is possible to update a row in one table, but not in the other. If your database supports transactions, you can group database operations into a transaction to prevent database inconsistency resulting from these events. If a failure occurs at one point in the transaction, all of the updates can be rolled back to their state before the transaction began. If no failures occur, the updates can be finalized by committing the transaction as complete.

In ADO.NET, you control transactions using the Connection and Transaction objects. You can initiate a local transaction using Connection.BeginTransaction. Once you have begun a transaction, you can enlist a command in that transaction using the Transaction property of the Command object. You can then use the Transaction object to commit or rollback modifications made at the data source based on the success or failure of the components of the transaction.

You can also enlist in an existing distributed transaction using Connection.EnlistDistributedTransaction. Enlisting in an existing distributed transaction ensures that if the entire distributed transaction is committed or rolled back, modifications made by code at the data source are committed or rolled back as well.

In This Section

Performing a Transaction Using ADO.NET
Explains how to use the Connection and Transaction objects to perform transactions against a data source.
Enlisting in a Distributed Transaction
Explains how to use the Connection object to enlist actions performed against a data source in a distributed transaction.

Related Sections

Using .NET Framework Data Providers to Access Data
Describes the components of the .NET Framework data provider and how it can be used to query and update data from a data source. Includes examples using the .NET Framework Data Provider for SQL Server and the .NET Framework Data Provider for OLE DB.
Show: