Dataset merge Method

Source: Internet
Author: User

Is the use of the merge method in msdn:

The merge method is used to merge two dataset objects with similar architectures. Merge applications on the clientProgramIt is usually used to merge the recent changes in the data source into the existing dataset. This allows client applications to have dataset refreshed with the latest data in the data source. The merge method is usually called at the end of a series of processes that involve verifying changes, removing errors, updating the data source using changes, and finally refreshing the existing dataset.
In client applications, there is usually a button that you can click to collect and verify the changed data, and then send it back to the middle layer component. In this case, the getchanges method is called first. This method returns another dataset optimized for verification and merging. The second DataSet object only contains the changed able and datarow objects. The result is a subset of the initial dataset. This subset is usually small, so it can be passed back to the middle layer components more efficiently. Then, the middle layer component updates the initial data source through the stored procedure using changes. Then, the middle layer can send back a new dataset, which contains the initial data and the latest data in the data source (by re-running the initial query ); or it can send back a subset that contains all changes made to it from the data source. (For example, if the data source automatically creates a unique primary key value, these values can be propagated back to the client application .) In which case, you can use the merge method to merge the returned dataset back to the initial dataset of the client application.
When a new source dataset is merged to the target, any source row 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 Added matches the new target row with the same primary key value as the new source row.
 

According to the above instructions, we know thatMergeWhen merging two data sets, the method compares the primary key values of rows. In this way, when a new row is added to the dataset, no problem exists. If the row is modified and the primary key value is not modified, however, if you modify the value of the primary key when changing the row, the problem arises.......The following is an example:

InSQL ServerNextProductsThe table structure is as follows:

 

InSQL ServerNextProductsThe table structure is as follows:

 

Column name

Data Type

Description

Code

Nchar

ProductCode(Primary Key Column)

Name

Nvarchar

Production name

Unitprice

Numeric

Unit Price

 

In. NetUsed inXSDGenerate a correspondingProductsdata. XSDThe structure is as follows:

 

column name

data type

description

Code

string

product code ( primary key column )

name

string

product name

unitprice

decimal

product unit price

 

AccordingMsdnInstructions, we passed at the front-endProductsdataAfter adding or modifying data, when submitting background updates, the common practice is as follows:

 

// Create a new dataset to save the changes made to the primary Dataset

 

Productsdata datasetchanges;

 

Datasetchanges = (productsdata) (productsdata. getchanges ());

 

// Check for any changes

 

If (datasetchanges! = NULL ){

 

Try {

 

// Make some changes, so try to call the update Method

 

// And pass the dataset 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 onVs. netBased on the above Code, we simulate adding a row of data to the dataset and updating it:

 

Code

Name

Unitprice

1001

Jinsha chocolate

120.00

 

No problem. Let's modify this line of data and then update it.CodeChange1002After the update, the result data does not show the original line as we expected.CodeColumn Value1001Change1002, But added a row:

 

Code

Name

Unitprice

1001

Jinsha chocolate

120.00

1002

Jinsha chocolate

130.00

 

Note: In general, we seldom change the primary key value, but when the code is not used, it is generally allowed to changeCodeEspecially in the system implementation phase.

 

The reason for the above problems is not surprising.MergeThis is also true for the principle of the method, but this is the result we don't want. How can we solve this problem? Do you not allow users to change the primary key, but it does not seem to conform to the actual situation. How can this problem be solved?

 

To useMergeMethod, it only depends onMergeThe principle of merging data is as follows. If we do not want to change the primary key, we will not change the primary key.ProductsdataAdd an additional primary key to the dataset.IDColumn, andAutoincrementSetTrueTo convert the original primary key columnCodeChangeKeyColumn, as for the backgroundSQL ServerThe source table is not modified. The modification is as follows:

 

Column name

Data Type

Description

ID

Int

Auto-increment column (primary key)

Code

String

Product Code (Key)

Name

String

Production name

Unitprice

Decimal

Unit Price

 

After this changeIDIs an auto-incrementing column. We do not modify its value, so we can change it at will.CodeColumn value,MergeBecause the method is compared based on the ID Example when merging data, there will no longer be the problem of adding multiple rows in front.

 

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.