Using a Transaction with a DataAdapter
Số trang: 4
Loại file: pdf
Dung lượng: 14.38 KB
Lượt xem: 1
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
[ Team LiB ] Recipe 6.5 Using a Transaction with a DataAdapter Problem You need to use a transaction when updating a data source using a DataAdapter. Solution Associate a Transaction with the appropriate Command object from the DataAdapter. The sample code contains three event handlers
Nội dung trích xuất từ tài liệu:
Using a Transaction with a DataAdapter[ Team LiB ]Recipe 6.5 Using a Transaction with a DataAdapterProblemYou need to use a transaction when updating a data source using a DataAdapter.SolutionAssociate a Transaction with the appropriate Command object from the DataAdapter.The sample code contains three event handlers:Form.Load 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 constantsusing 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, ConfigurationSettings.AppSettings[Sql_ConnectString]); // 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); da.Fill(dt); // 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( ConfigurationSettings.AppSettings[Sql_ConnectString]); 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. try { // Submit the changes. da.Update(dt); // Success. Commit. tran.Commit( ); } catch (Exception ex) { // Exception. Roll back. tran.Rollback( ); MessageBox.Show(ex.Message + Environment.NewLine + Transaction rolled back.); } finally { conn.Close( ); }}private void refreshButton_Click(object sender, System.EventArgs e){ // Refresh the orders data. dt.Clear( ); da.Fill(dt);}DiscussionYou can use a transaction with a DataAdapter to allow the roll back of updates made bythe DataAdapter in the event of an error.If, as in the solution, a CommandBuilder is used to generate the update logic for theDataAdapter, associate the Transaction with the SelectCommand of the DataAdapter asshown in the solution code:da.SelectCommand.Transaction = tran;If custom update logic is used for the DataAdapter, the Transaction must be associatedwith 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;[ Team LiB ]
Nội dung trích xuất từ tài liệu:
Using a Transaction with a DataAdapter[ Team LiB ]Recipe 6.5 Using a Transaction with a DataAdapterProblemYou need to use a transaction when updating a data source using a DataAdapter.SolutionAssociate a Transaction with the appropriate Command object from the DataAdapter.The sample code contains three event handlers:Form.Load 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 constantsusing 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, ConfigurationSettings.AppSettings[Sql_ConnectString]); // 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); da.Fill(dt); // 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( ConfigurationSettings.AppSettings[Sql_ConnectString]); 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. try { // Submit the changes. da.Update(dt); // Success. Commit. tran.Commit( ); } catch (Exception ex) { // Exception. Roll back. tran.Rollback( ); MessageBox.Show(ex.Message + Environment.NewLine + Transaction rolled back.); } finally { conn.Close( ); }}private void refreshButton_Click(object sender, System.EventArgs e){ // Refresh the orders data. dt.Clear( ); da.Fill(dt);}DiscussionYou can use a transaction with a DataAdapter to allow the roll back of updates made bythe DataAdapter in the event of an error.If, as in the solution, a CommandBuilder is used to generate the update logic for theDataAdapter, associate the Transaction with the SelectCommand of the DataAdapter asshown in the solution code:da.SelectCommand.Transaction = tran;If custom update logic is used for the DataAdapter, the Transaction must be associatedwith 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;[ Team LiB ]
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Using a Transaction with a DataAdapterGợi ý tài liệu liên quan:
-
52 trang 430 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 314 0 0 -
74 trang 299 0 0
-
96 trang 293 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 289 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 281 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 275 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 265 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 265 0 0