How to: Save Data by Using a Transaction
You save data in a transaction using the System.Transactions namespace. Use the TransactionScope object to participate in a transaction that is automatically managed for you.
Projects are not created with a reference to the System.Transactions assembly, so you need to manually add a reference to projects that use transactions.
Note
The System.Transactions namespace is supported on Windows 2000 and later.
The easiest way to implement a transaction is to instantiate a TransactionScope object in a using statement. (For more information, see Using Statement (Visual Basic), and using Statement (C# Reference).) The code executed within the using statement will participate in the transaction.
To commit the transaction, call the Complete method as the last statement in the using block.
To roll back the transaction, throw an exception prior to calling the Complete method.
For more information, see Walkthrough: Saving Data in a Transaction.
To add a reference to the System.Transactions dll
From the Project menu, choose Add Reference.
Select System.Transactions on the .NET tab (SQL Server tab for SQL Server projects) and click OK.
A reference to System.Transactions.dll is added to the project.
To save data in a transaction
Add code to save data within the using statement that contains the transaction. The following code shows how to create and instantiate a TransactionScope object in a using statement:
Using updateTransaction As New Transactions.TransactionScope ' Add code to save your data here. ' Throw an exception to roll back the transaction. ' Call the Complete method to commit the transaction updateTransaction.Complete() End Using
using (System.Transactions.TransactionScope updateTransaction = new System.Transactions.TransactionScope()) { // Add code to save your data here. // Throw an exception to roll back the transaction. // Call the Complete method to commit the transaction updateTransaction.Complete(); }
See Also
Tasks
Walkthrough: Saving Data in a Transaction
Concepts
Binding Windows Forms Controls to Data in Visual Studio
Preparing Your Application to Receive Data
Fetching Data into Your Application
Binding Controls to Data in Visual Studio
Editing Data in Your Application