The Merge of the DataSet ()

Source: Internet
Author: User
When we merge two identical or similar dataset objects in ADO. NET, we typically use the dataset's Merge method, which has multiple overloaded versions, and we review the merge method before introducing it, and here's how to use the merge method in MSDN:

The merge method is used to consolidate two DataSet objects that are roughly similar to the schema . Merging on a client application is typically used to merge the most recent changes in the data source into an existing dataset. This enables the client application to have a dataset refreshed with the latest data from the data source. The Merge method is typically called at the end of a series of procedures involving validating changes, eliminating errors, updating the data source with changes, and finally refreshing the existing dataset. In a client application, there is usually a button that the user can click to collect and validate the changed data, and then send it back to the middle tier component. In this case, the GetChanges method is invoked first . This method returns another DataSet that is optimized for validation and merging. The second DataSet object contains only the changed DataTable and DataRow objects, resulting in a subset of the initial dataset. This subset is usually small, so it can be passed back to the middle tier component more efficiently. The middle-tier component then uses the changes to update the initial data source through the stored procedure. The middle tier can then send back a new dataset that contains the initial data and the latest data from the data source (by running the initial query again), or it can send back a subset of all changes made to it from the data source. (for example, if your data source automatically creates unique primary key values, you can propagate these values back to the client application.) In either case, you can use the Merge method to merge the returned dataset back into the client application's initial dataset. When a new source DataSet is merged into the target, any source rows with the DataRowState value of unchanged, Modified, or Deleted will match the target row with the same primary key value. The source row with the DataRowState value of Added matches the new target row with the same primary key value as the Xinyuan row.
Based on the instructions above, we know that the merge method combines the primary key value of the row with the main contrast reference when merging two datasets. This does not have any problems adding new rows to the dataset, but it doesn't matter if you modify the row without modifying the primary key value, but if you modify the value of the primary key when you change the line, the problem comes ... Here's an example: a Products table structure under SQL Server is as follows:
Column Name Data Type Description
Code NChar Product Code (primary key column)
Name nvarchar Name of production name
UnitPrice Numeric Product price
Using XSD in. NET to generate a corresponding productsdata.xsd structure is as follows:
column name data type Description
Code string Product code (primary key column)
Name string product name
UnitPrice decimal product price
As explained by MSDN, when we submit background updates after the foreground is added or modified via productsdata, it is usually done as follows://Create a new dataset to hold changes to the main dataset Productsdata Datasetcha         Nges;         Datasetchanges = (productsdata) (Productsdata.getchanges ()); Check if any changes have been made if (datasetchanges!= null) {try {//need to make some changes, so try calling the Update method//and passing the number According to the set and any parameters to update the data source Updatedatasource (datasetchanges); Productsdata.merge (datasetchanges);Productsdata.acceptchanges ();             catch (System.Exception eupdate) {throw eupdate; The above code is based on the Vs.net data form Generation Wizard, which, based on the above code, simulates adding a row of data to a dataset and updating it:
Code Name UnitPrice
1001 Golden Sand Chocolate 120.00
No problem, the following we modify this line of data to update, here we change the Code to 1002, after the update result data did not as we expected the original line of the Code column of the value 1001 to 1002, but added a row:
Code Name UnitPrice
1001 Golden Sand Chocolate 120.00
1002 Golden Sand Chocolate 130.00
Note: Normally we rarely change the primary key value, but in the case where the code is not used, it is generally allowed to change code, especially at the stage of system implementation. The cause of the above problem is not surprising, according to the principle of the Merge method, this is true, but this is what we do not want, how to solve it, do not allow the user to change the primary key, but it seems to be not realistic. How to solve it. Now that you want to use the Merge method, that only according to merge data principle to do, not to change the primary key we do not change the primary key, we can give the foreground of the Productsdata DataSet add an additional primary key ID column, and its AutoIncrement set to True, the original master The key column Code is changed to the key column, and the source table in the background SQL Server is not modified, as follows:
column name data type Description
IDs Int Automatic growth column (primary key)
Code String Product Code    (key key)

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.