You can merge the contents of one dataset (referred to as the source dataset) into another dataset (referred to as the target dataset). Some typical examples of when you would perform a merge are:
- You have a main dataset containing your working data, and you get a second dataset containing updates or records to be added to it. This is a typical scenario when you are working with an XML Web service that returns a dataset to you as the result of a method call.
- Your application gets a dataset from two different components but needs to work with the data in a single dataset. For example, you might get company information from one component and stock price information from another.
To copy information from one dataset to another, you use a dataset's Merge method. You can specify merge options to achieve a variety of goals:
- Add new records to one dataset by copying them from another dataset.
- Update the state of records in one dataset based on another. In this situation, the second dataset contains information not only about new records, but also changed records and deleted records, and you want your first dataset to reflect all these changes.
- Copy not only new records and changes, but specify what to do if the datasets do not have the same schemas.
To merge one dataset into another
- Call the Merge method of the target dataset, passing it the source dataset containing the records to be merged.
The following example shows how to merge two datasets with the default setting of PreserveChanges = false:
' Visual Basic targetDataset.Merge(sourceDataset) // C# targetDataset.Merge(sourceDataset);
The following example shows how to merge two datasets with the PreserveChanges argument set to true:
' Visual Basic targetDataset.Merge(sourceDataset, True) // C# targetDataset.Merge(sourceDataset, true);