Ado. Analysis of most of the table operations in net-modification
Source: Internet
Author: User
ado| data ADO. Analysis of most of the table operations in net-modification
Author: Zhengzo??????? 2004-8-5
Iii. Updating data sets
The first thing to note is that I've removed the Order Details table, and the two-table operation is just a few of the fields. The following is the form interface:
Figure 3-1
The radio box is used to select different update methods.
Add two class member variables to the DataAccess class:
???? Private SqlDataAdapter _customerdataadapter; Customer Data Adapters
???? Private SqlDataAdapter _orderdataadapter; Order data Adapters
?
Customerdataadapter in constructors is initialized to
Instantiation of _customerdataadapter
SqlCommand Selectcustomercomm = new SqlCommand ("GetCustomer", _conn);
The code above can be built with a designer, and it feels better to write something yourself, but there are still a lot of code.
For _orderdataadapter initialization is similar to the above, here we only look at the processing of order increase, the following is the stored procedure:
CREATE PROCEDURE? AddOrder
(
???? @OrderID INT out,
???? @CustomerID NCHAR (5),
???? @OrderDate DATETIME
)
As
INSERT into Orders
(
???? CustomerID,
???? OrderDate
)
VALUES
(
???? @CustomerID,
???? @OrderDate
)
--select @OrderID = @ @IDENTITY
SET @OrderID = scope_identity ()
Go
?
OrderID The acquisition of automatic growth values is done through output parameters, which is pretty good, and it's very inefficient if you use the Sqldataadapter.rowupdated event to handle it.
The Insertordercomm object is defined as:
SqlCommand Insertordercomm = new SqlCommand ("AddOrder", _conn);
Before implementing the Update method for the data, let's clarify some of the update logic:
For rows marked for deletion, delete the data from the order table, and then delete the data from the Customer table;
For rows marked for addition, add the data for the Customer table before adding the order table data.
?
(1) Implement a method for updating data by obtaining a subset of replicas of the modified dataset.
This is also a common way to invoke XML Web service update data, first to look at the first version, the subset of the acquisition by the Dataset.getchangs method to complete.
Updating data by using a data collection
public void Updatecustomerorders (Datasetorders ds)
{????????????
???? DataSet dsmodified = ds. GetChanges (datarowstate.modified);//Get Modified rows
???? DataSet dsdeleted = ds. GetChanges (datarowstate.deleted);//Get the row marked for deletion
???? DataSet dsadded = ds. GetChanges (datarowstate.added);//Get Increased rows
???? Try
???? {???
???????? _conn. Open (), add the Customer table data, and then add the Order table data
???????? throw new Exception ("Update data Error", ex);
???? }
???? Finally
???? {
???????? if (_conn. State!= connectionstate.closed)
????????????? _conn. Close ();
???? }
}
The above method looks clearer, but not very efficient, with at least three datasets created in the middle, and then multiple merges.
(2) Another method is to refer to the update without creating a copy.
Performance can be a lot higher, but the amount of data that is transferred on a Web service can be greater (combined with two methods to improve). The concrete implementation is to select the row state through the DataTable.Select method.
Updating data by reference method
public void Updatecustomerorders (DataSet ds)
{
???? Try
???? {???
???????? _conn. Open ();
???????? Add the Customer table data First, then add the order table data???? _customerdataadapter.update (ds. tables["Customers"]. Select ("", "", dataviewrowstate.added));
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service