Export (0) Print
Expand All

DataSet.Merge Method (DataSet, Boolean, MissingSchemaAction)

Merges a specified DataSet and its schema with the current DataSet, preserving or discarding changes in the current DataSet and handling an incompatible schema according to the given arguments.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)

'Declaration
Public Sub Merge ( _
	dataSet As DataSet, _
	preserveChanges As Boolean, _
	missingSchemaAction As MissingSchemaAction _
)
'Usage
Dim instance As DataSet 
Dim dataSet As DataSet 
Dim preserveChanges As Boolean 
Dim missingSchemaAction As MissingSchemaAction

instance.Merge(dataSet, preserveChanges, _
	missingSchemaAction)

Parameters

dataSet
Type: System.Data.DataSet

The DataSet whose data and schema will be merged.

preserveChanges
Type: System.Boolean

true to preserve changes in the current DataSet; otherwise false.

missingSchemaAction
Type: System.Data.MissingSchemaAction

One of the MissingSchemaAction values.

ExceptionCondition
ArgumentNullException

The dataSet is Nothing.

The Merge method is used to merge two DataSet objects that have largely similar schemas. A merge is typically used on a client application to incorporate the latest changes from a data source into an existing DataSet. This allows the client application to have a refreshed DataSet with the latest data from the data source.

The Merge method is typically called at the end of a series of procedures that involve validating changes, reconciling errors, updating the data source with the changes, and finally refreshing the existing DataSet.

In a client application, it is common to have a single button that the user can click that gathers the changed data and validates it before sending it back to a middle-tier component. In this scenario, the GetChanges method is first invoked. That method returns a second DataSet optimized for validating and merging. This second DataSet object contains only the DataTable and DataRow objects that were changed, resulting in a subset of the original DataSet. This subset is generally smaller, and thus more efficiently passed back to a middle-tier component. The middle-tier component then updates the original data source with the changes through stored procedures. The middle tier can then send back either a new DataSet that includes original data and the latest data from the data source (by running the original query again), or it can send back the subset with any changes that have been made to it from the data source. (For example, if the data source automatically creates unique primary key values, these values can be propagated back to the client application.) In either case, the returned DataSet can be merged back into the client application's original DataSet with the Merge method.

To facilitate explanation of the Merge method, we use "target" to signify the current DataSet, and "source" to name the second (parameter) DataSet. The target DataSet is so named because it is the object upon which an action (the merge) occurs. The second DataSet is called a "source" because the information it contains does not change, but instead is merged into the current DataSet.

When the Merge method is called, the schemas of the two DataSet objects are compared because it is possible that the schemas may have been changed. For example, in a business-to-business scenario, new columns may have been added to an XML schema by an automated process. If the source DataSet contains schema elements (added DataColumn objects) that are missing in the target, the schema elements can be added to the target by setting the missingSchemaAction argument to MissingSchemaAction.Add. In that case, the merged DataSet contains the added schema and data.

After merging schemas, the data is merged.

When merging a new source DataSet into the target, any source rows with a DataRowState value of Unchanged, Modified, or Deleted are matched to target rows with the same primary key values. Source rows with a DataRowState value of Added are matched to new target rows with the same primary key values as the new source rows.

During a merge, constraints are disabled. If any constraints cannot be enabled at the end of merge, a ConstraintException is generated and the merged data is retained while the constraints are disabled. In this case, the EnforceConstraints property is set to false, and all rows that are invalid are marked in error. The errors must be resolved before attempting to reset the EnforceConstraints property to true.

The following example creates a simple DataSet with one table, two columns, and ten rows. Two values are changed, and one row is added. A subset of the changed data is created using the GetChanges method. After reconciling errors, a new column is added to the subset, changing the schema. When the Merge method is called with the missingSchemaAction set to MissingSchemaAction.Add, the new column is added to the original DataSet object's schema.

Private Sub DemonstrateMergeMissingSchema()
    ' Create a DataSet with one table, two columns,  
    ' and three rows. 
    Dim dataSet As New DataSet("dataSet")
    Dim table As New DataTable("Items")
    Dim idColumn As New DataColumn("id", _
        Type.GetType("System.Int32"))
    idColumn.AutoIncrement = True 
    Dim itemColumn As New DataColumn("Item", _
        Type.GetType("System.Int32"))

    ' DataColumn array to set primary key. 
    Dim keyColumn(1) As DataColumn
    Dim row As DataRow

    ' Create variable for temporary DataSet.  
    Dim changeDataSet As DataSet

    ' Add RowChanged event handler for the table. 
    AddHandler table.RowChanged, AddressOf Row_Changed
    dataSet.Tables.Add(table)
    table.Columns.Add(idColumn)
    table.Columns.Add(itemColumn)

    ' Set primary key column.
    keyColumn(0) = idColumn
    table.PrimaryKey = keyColumn

    ' Add ten rows. 
    Dim i As Integer 
    For i = 0 To 9
        row = table.NewRow()
        row("Item") = i
        table.Rows.Add(row)
    Next i

    ' Accept changes.
    dataSet.AcceptChanges()
    PrintValues(dataSet, "Original values")

    ' Change row values.
    table.Rows(0)("Item") = 50
    table.Rows(1)("Item") = 111

    ' Add one row.
    row = table.NewRow()
    row("Item") = 74
    table.Rows.Add(row)

    ' Insert code for error checking. Set one row in error.
    table.Rows(1).RowError = "over 100"
    PrintValues(dataSet, "Modified and New Values")
    ' If the table has changes or errors,  
    ' create a subset DataSet. 
    If dataSet.HasChanges(DataRowState.Modified Or DataRowState.Added) _
        And dataSet.HasErrors Then 
        ' Use GetChanges to extract subset.
        changeDataSet = dataSet.GetChanges(DataRowState.Modified _
            Or DataRowState.Added)
        PrintValues(changeDataSet, "Subset values")
        ' Insert code to reconcile errors. In this case, reject changes. 
        Dim changeTable As DataTable
        For Each changeTable In  changeDataSet.Tables
            If changeTable.HasErrors Then 
                Dim changeRow As DataRow
                For Each changeRow In  changeTable.Rows
                    If CInt(changeRow("Item", _
                        DataRowVersion.Current)) > 100 Then
                        changeRow.RejectChanges()
                        changeRow.ClearErrors()
                    End If 
                Next changeRow
            End If 
        Next changeTable

        ' Add a column to the changeDataSet to change the schema.
        changeDataSet.Tables("Items").Columns.Add( _
            New DataColumn("newColumn"))
        PrintValues(changeDataSet, "Reconciled subset values")

        ' Add values to the rows for each column. 
        Dim rowItem As DataRow
        For Each rowItem In  changeDataSet.Tables("Items").Rows
            rowItem("newColumn") = "my new schema value" 
        Next rowItem

        ' Merge changes back to first DataSet.
        dataSet.Merge(changeDataSet, False, _
            System.Data.MissingSchemaAction.Add)
    End If
    PrintValues(dataSet, "Merged Values")
End Sub 

Private Sub Row_Changed(sender As Object, _
    e As DataRowChangeEventArgs)
     Console.WriteLine("Row Changed " & e.Action.ToString() _
        & ControlChars.Tab & e.Row.ItemArray(0).ToString())
End Sub 

Private Sub PrintValues(dataSet As DataSet, label As String)
    Console.WriteLine(ControlChars.Cr & label)
    Dim table As DataTable
    For Each table In  dataSet.Tables
        Console.WriteLine("TableName: " & table.TableName)
        Dim row As DataRow
        For Each row In  table.Rows
            Dim column As DataColumn
            For Each column In  table.Columns
                Console.Write(ControlChars.Tab & " " _
                    & row(column).ToString())
            Next column
            Console.WriteLine()
        Next row
    Next table
End Sub

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft