Using a Transaction with a DataAdapter


You need to use a transaction when updating a data source using a DataAdapter .


Associate a Transaction with the appropriate Command object from the DataAdapter .

The sample code contains three event handlers:


Sets up the sample by using a DataAdapter to load a DataTable with the Orders table from the Northwind database. A CommandBuilder is used to generate the updating logic. The default view of the DataTable is bound to a data grid on the form.

Update Button.Click

Creates a new Transaction object on the Connection of the SelectCommand of the DataAdapter . The Transaction is associated with the Connection objects for the update commands generated for the DataAdapter by the CommandBuilder . The Update( ) method of the DataAdapter is called to update DataTable changes to the Orders table. If no errors are encountered , the transaction is committed; otherwise , all changes made are rolled back.

Refresh Button.Click

Clears and reloads the Orders DataTable .

The C# code is shown in Example 6-7.

Example 6-7. File: TransactionDataAdapter.cs

// Namespaces, variables, and constants
using System;
using System.Configuration;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

private const String ORDERS_TABLE = "Orders";

private DataTable dt;
private SqlDataAdapter da;
private SqlCommandBuilder cb;

// . . . 

private void TransactionDataAdapterForm_Load(object sender,
 System.EventArgs e)
 String sqlText = "SELECT * FROM Orders";

 // Fill the Orders table for editing.
 da = new SqlDataAdapter(sqlText,
 // Stop updating when an error is encountered for roll back.
 da.ContinueUpdateOnError = false;
 // Create CommandBuilder and generate updating logic.
 cb = new SqlCommandBuilder(da);
 cb.GetDeleteCommand( );
 cb.GetInsertCommand( );
 cb.GetUpdateCommand( );
 // Create table and fill with orders schema and data.
 dt = new DataTable(ORDERS_TABLE);
 da.FillSchema(dt, SchemaType.Source);

 // Bind the default view of the table to the grid.
 dataGrid.DataSource = dt.DefaultView;

private void updateButton_Click(object sender, System.EventArgs e)
 // Create and open the connection.
 SqlConnection conn = new SqlConnection(
 conn.Open( );

 // Create and start the transaction.
 SqlTransaction tran = null;
 tran = conn.BeginTransaction( );

 // Associate CommandBuilder generated update commands
 // with the transaction.
 da.SelectCommand.Transaction = tran;

 // Update the data source.
 // Submit the changes.

 // Success. Commit.
 tran.Commit( );
 catch (Exception ex)
 // Exception. Roll back.
 tran.Rollback( );

 MessageBox.Show(ex.Message + Environment.NewLine +
 "Transaction rolled back.");
 conn.Close( );

private void refreshButton_Click(object sender, System.EventArgs e)
 // Refresh the orders data.
 dt.Clear( );


You can use a transaction with a DataAdapter to allow the roll back of updates made by the DataAdapter in the event of an error.

If, as in the solution, a CommandBuilder is used to generate the update logic for the DataAdapter , associate the Transaction with the SelectCommand of the DataAdapter as shown in the solution code:

da.SelectCommand.Transaction = tran;

If custom update logic is used for the DataAdapter , the Transaction must be associated with the DeleteCommand , InsertCommand , and UpdateCommand of the DataAdapter , but not the SelectCommand , as shown in the following code:

da.DeleteCommand.Transaction = tran;
da.InsertCommand.Transaction = tran;
da.UpdateCommand.Transaction = tran;

Connecting to Data

Retrieving and Managing Data

Searching and Analyzing Data

Adding and Modifying Data

Copying and Transferring Data

Maintaining Database Integrity

Binding Data to .NET User Interfaces

Working with XML

Optimizing .NET Data Access

Enumerating and Maintaining Database Objects

Appendix A. Converting from C# to VB Syntax

ADO. NET Cookbook
ADO.NET 3.5 Cookbook (Cookbooks (OReilly))
ISBN: 0596101406
EAN: 2147483647
Year: 2002
Pages: 222
Authors: Bill Hamilton © 2008-2020.
If you may any questions please contact us: