Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu Dealing with Update Failures pptx
Nội dung xem thử
Mô tả chi tiết
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
HandlingUpdateFailures.cs 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 CustomerID = @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
CustomerID 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.