tailieunhanh - Dealing with Update Failures

Dealing with Update Failures So far, the examples you've seen have assumed that the updates pushed to the database by the Update() have succeeded. | Dealing with Update Failures So far the examples you ve seen have assumed that the updates pushed to the database by the Update have succeeded. In this section you see what happens when updates fail-and what you can do about it. Note You ll find all the code examples shown in this section in the file located in the ch11 directory. This program listing is omitted from this book for brevity. In the examples in this section assume that the CommandText property of a SqlDataAdapter object s UpdateCommand is set as follows UPDATE Customers SET CompanyName @NewCompanyName Address @NewAddress WHERE CustomerlD @OldCustomerID AND CompanyName @OldCompanyName AND Address @OldAddress This UPDATE statement uses optimistic concurrency because the updated columns are included in the WHERE clause. An Update Failure Scenario Consider the following scenario that shows an update failure 1. User 1 retrieves the rows from the Customers table into a DataTable named customersDataTable. 2. User 2 retrieves the same rows. 3. User 1 updates the CustomerName DataColumn of the DataRow with the CustomerlD DataColumn of J5COM and pushes the change to the database. Let s say User 1 changes the CustomerName from J5 Company to Updated Company. 4. User 2 updates the same DataRow and changes the CompanyName from J5 Company to Widgets Inc. and attempts to push the change to the database. User 2 then causes a DBConcurrecy-Exception object to be thrown and their update fails. The same exception occurs if User 2 tries to update or delete a row that has already been deleted by User 1. Why does the update fail in step 4 The reason is that with optimistic concurrency the CompanyName column is used in the WHERE clause of the UPDATE statement. Because of this the original row loaded by User 2 cannot be found anymore-and therefore the UPDATE statement fails. The row cannot be found because User 1 has already changed the CompanyName column from J5 Company to Updated Company in step 2.

TÀI LIỆU LIÊN QUAN
10    127    1
6    150    1
7    127    1
5    125    1
6    127    1
6    115    1
6    122    1
6    174    1
7    122    1
5    97    1
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.