Using Transactions with a DataSet (SQL)
Số trang: 2
Loại file: pdf
Dung lượng: 14.07 KB
Lượt xem: 8
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:
Using Transactions with a DataSet (SQL) In Chapter 3, "Introduction to Structured Query Language," you saw how you can group SQL statements together into transactions.
Nội dung trích xuất từ tài liệu:
Using Transactions with a DataSet (SQL)Using Transactions with a DataSet (SQL)In Chapter 3, Introduction to Structured Query Language, you saw how you can groupSQL statements together into transactions. The transaction is then committed or rolledback as one unit. For example, in the case of a banking transaction, you might want towithdraw money from one account and deposit it into another account. You would thencommit both of these changes as one unit, or if theres a problem, rollback both changes.In Chapter 8, Executing Database Commands, you saw how to use a Transaction objectto represent a transaction.As you know, a DataSet doesnt have a direct connection to the database. Instead, you usethe Fill() and Update() methods of a DataAdapter to pull and push rows from and to thedatabase to your DataSet respectively. In fact, a DataSet has no knowledge of thedatabase at all. A DataSet simply stores a disconnected copy of the data. Because of this,a DataSet doesnt have any built-in functionality to handle transactions.How then do you use transactions with a DataSet? The answer is you must use theTransaction property of the Command objects stored in a DataAdapter.Using the DataAdapter Command Objects Transaction PropertyA DataAdapter stores four Command objects that you access using the SelectCommand,InsertCommand, UpdateCommand, and DeleteCommand properties. When you call theUpdate() method of a DataAdapter, it runs the appropriate InsertCommand,UpdateCommand, or DeleteCommand.You can create a Transaction object and set the Transaction property of the Commandobjects in your DataAdapter to this Transaction object. When you then modify yourDataSet and push the changes to the database using the Update() method of yourDataAdapter, the changes will use the same Transaction.The following example creates a SqlTransaction object named mySqlTransaction and setsthe Transaction property of each of the Command objects in mySqlDataAdapter tomySqlTransaction:SqlTransaction mySqlTransaction = mySqlConnection.BeginTransaction();mySqlDataAdapter.SelectCommand.Transaction = mySqlTransaction;mySqlDataAdapter.InsertCommand.Transaction = mySqlTransaction;mySqlDataAdapter.UpdateCommand.Transaction = mySqlTransaction;mySqlDataAdapter.DeleteCommand.Transaction = mySqlTransaction;Each of the Command objects in mySqlDataAdapter will now use mySqlTransaction.Lets say you added, modified, and removed some rows from a DataTable contained in aDataSet named myDataSet. You can push these changes to the database using thefollowing example:mySqlDataAdapter.Update(myDataSet);All your changes to myDataSet are pushed to the database as part of the transaction inmySqlTransaction. You can commit those changes using the Commit() method ofmySqlTransaction:mySqlTransaction.Commit();You could also roll back those changes using the Rollback() method ofmySqlTransaction.Note A transaction is rolled back by default; therefore, you should always explicitly commit or roll back your transaction using Commit() or Rollback() to make it clear what your program is intended to do.
Nội dung trích xuất từ tài liệu:
Using Transactions with a DataSet (SQL)Using Transactions with a DataSet (SQL)In Chapter 3, Introduction to Structured Query Language, you saw how you can groupSQL statements together into transactions. The transaction is then committed or rolledback as one unit. For example, in the case of a banking transaction, you might want towithdraw money from one account and deposit it into another account. You would thencommit both of these changes as one unit, or if theres a problem, rollback both changes.In Chapter 8, Executing Database Commands, you saw how to use a Transaction objectto represent a transaction.As you know, a DataSet doesnt have a direct connection to the database. Instead, you usethe Fill() and Update() methods of a DataAdapter to pull and push rows from and to thedatabase to your DataSet respectively. In fact, a DataSet has no knowledge of thedatabase at all. A DataSet simply stores a disconnected copy of the data. Because of this,a DataSet doesnt have any built-in functionality to handle transactions.How then do you use transactions with a DataSet? The answer is you must use theTransaction property of the Command objects stored in a DataAdapter.Using the DataAdapter Command Objects Transaction PropertyA DataAdapter stores four Command objects that you access using the SelectCommand,InsertCommand, UpdateCommand, and DeleteCommand properties. When you call theUpdate() method of a DataAdapter, it runs the appropriate InsertCommand,UpdateCommand, or DeleteCommand.You can create a Transaction object and set the Transaction property of the Commandobjects in your DataAdapter to this Transaction object. When you then modify yourDataSet and push the changes to the database using the Update() method of yourDataAdapter, the changes will use the same Transaction.The following example creates a SqlTransaction object named mySqlTransaction and setsthe Transaction property of each of the Command objects in mySqlDataAdapter tomySqlTransaction:SqlTransaction mySqlTransaction = mySqlConnection.BeginTransaction();mySqlDataAdapter.SelectCommand.Transaction = mySqlTransaction;mySqlDataAdapter.InsertCommand.Transaction = mySqlTransaction;mySqlDataAdapter.UpdateCommand.Transaction = mySqlTransaction;mySqlDataAdapter.DeleteCommand.Transaction = mySqlTransaction;Each of the Command objects in mySqlDataAdapter will now use mySqlTransaction.Lets say you added, modified, and removed some rows from a DataTable contained in aDataSet named myDataSet. You can push these changes to the database using thefollowing example:mySqlDataAdapter.Update(myDataSet);All your changes to myDataSet are pushed to the database as part of the transaction inmySqlTransaction. You can commit those changes using the Commit() method ofmySqlTransaction:mySqlTransaction.Commit();You could also roll back those changes using the Rollback() method ofmySqlTransaction.Note A transaction is rolled back by default; therefore, you should always explicitly commit or roll back your transaction using Commit() or Rollback() to make it clear what your program is intended to do.
Tìm kiếm theo từ khóa liên quan:
kĩ thuật lập trình công nghệ thông tin lập trình ngôn ngữ lập trình C Shark C# sybex - c.sharp database programming Using Transactions with a DataSet (SQL)Gợi ý tài liệu liên quan:
-
52 trang 413 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 294 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 287 0 0 -
96 trang 279 0 0
-
74 trang 277 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 266 1 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 263 0 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 258 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 254 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 247 0 0