本文整理汇总了VB.NET中System.Data.DataSet.Merge方法的典型用法代码示例。如果您正苦于以下问题:VB.NET DataSet.Merge方法的具体用法?VB.NET DataSet.Merge怎么用?VB.NET DataSet.Merge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.DataSet
的用法示例。
在下文中一共展示了DataSet.Merge方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: DemonstrateMerge
Private Sub DemonstrateMerge()
' 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(0) As DataColumn
Dim row As DataRow
' Create variable for temporary DataSet.
Dim changeDataSet As DataSet
' Add columns to table, and table to DataSet.
table.Columns.Add(idColumn)
table.Columns.Add(itemColumn)
dataSet.Tables.Add(table)
' 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 two 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
'Console.WriteLine(changeRow["Item"]);
If CInt(changeRow("Item", _
DataRowVersion.Current)) > 100 Then
changeRow.RejectChanges()
changeRow.ClearErrors()
End If
Next changeRow
End If
Next changeTable
PrintValues(changeDataSet, "Reconciled subset values")
' Merge changes back to first DataSet.
dataSet.Merge(changeDataSet)
PrintValues(dataSet, "Merged Values")
End If
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
示例2: DemonstrateMergeTable
Private Sub DemonstrateMergeTable()
' Create a DataSet with one table, two columns,
' and ten rows.
Dim dataSet As New DataSet("dataSet")
Dim table As New DataTable("Items")
' Add tables to the DataSet
dataSet.Tables.Add(table)
' Add columns
Dim c1 As New DataColumn("id", Type.GetType("System.Int32"), "")
Dim c2 As New DataColumn("Item", Type.GetType("System.Int32"), "")
table.Columns.Add(c1)
table.Columns.Add(c2)
' DataColumn array to set primary key.
Dim keyCol(0) As DataColumn
' Set primary key column.
keyCol(0) = c1
table.PrimaryKey = keyCol
' Add RowChanged event handler for the table.
AddHandler table.RowChanged, AddressOf Row_Changed
' Add ten rows.
Dim i As Integer
Dim row As DataRow
For i = 0 To 9
row = table.NewRow()
row("id") = i
row("Item") = i
table.Rows.Add(row)
Next i
' Accept changes.
dataSet.AcceptChanges()
PrintValues(dataSet, "Original values")
' Create a second DataTable identical to the first.
Dim t2 As DataTable
t2 = table.Clone()
' Add three rows. Note that the id column can't be the
' same as existing rows in the DataSet table.
Dim newRow As DataRow
newRow = t2.NewRow()
newRow("id") = 14
newRow("Item") = 774
t2.Rows.Add(newRow)
newRow = t2.NewRow()
newRow("id") = 12
newRow("Item") = 555
t2.Rows.Add(newRow)
newRow = t2.NewRow()
newRow("id") = 13
newRow("Item") = 665
t2.Rows.Add(newRow)
' Merge the table into the DataSet.
Console.WriteLine("Merging")
dataSet.Merge(t2)
PrintValues(dataSet, "Merged With Table")
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
Dim row As DataRow
Dim column As DataColumn
For Each table In dataSet.Tables
Console.WriteLine("TableName: " & table.TableName)
For Each row In table.Rows
For Each column In table.Columns
Console.Write(ControlChars.Tab & " " _
& row(column).ToString())
Next column
Console.WriteLine()
Next row
Next table
End Sub
示例3: DemonstrateMerge
Private Sub DemonstrateMerge()
' 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"), "")
' Create DataColumn array to set primary key.
Dim keyColumn(0) As DataColumn
Dim row As DataRow
' Create variable for temporary DataSet.
Dim changesDataSet 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.
changesDataSet = dataSet.GetChanges( _
DataRowState.Modified Or DataRowState.Added)
PrintValues(changesDataSet, "Subset values")
' Insert code to reconcile errors. In this case, reject changes.
Dim changesTable As DataTable
For Each changesTable In changesDataSet.Tables
If changesTable.HasErrors Then
Dim changesRow As DataRow
For Each changesRow In changesTable.Rows
'Console.WriteLine(changesRow["Item"]);
If CInt(changesRow("Item", _
DataRowVersion.Current)) > 100 Then
changesRow.RejectChanges()
changesRow.ClearErrors()
End If
Next changesRow
End If
Next changesTable
' Add a column to the changesDataSet.
changesDataSet.Tables("Items").Columns.Add( _
New DataColumn("newColumn"))
PrintValues(changesDataSet, "Reconciled subset values")
' Merge changes back to first DataSet.
dataSet.Merge(changesDataSet, 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(label + ControlChars.Cr)
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
示例4: DemonstrateMergeMissingSchema
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(0) 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
示例5: DemonstrateMergeTableAddSchema
Private Sub DemonstrateMergeTableAddSchema()
' Create a DataSet with one table, two columns,
'and ten rows.
Dim dataSet As New DataSet("dataSet")
Dim table As New DataTable("Items")
' Add tables to the DataSet
dataSet.Tables.Add(table)
' Create and add two columns to the DataTable
Dim idColumn As New DataColumn("id", _
Type.GetType("System.Int32"), "")
idColumn.AutoIncrement = True
Dim itemColumn As New DataColumn("Item", _
Type.GetType("System.Int32"), "")
table.Columns.Add(idColumn)
table.Columns.Add(itemColumn)
' DataColumn array to set primary key.
Dim keyCol(0) As DataColumn
' Set primary key column.
keyCol(0) = idColumn
table.PrimaryKey = keyCol
' Add RowChanged event handler for the table.
AddHandler table.RowChanged, AddressOf Row_Changed
' Add ten rows.
Dim i As Integer
Dim row As DataRow
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")
' Create a second DataTable identical to the first
' with one extra column using the Clone method.
Dim cloneTable As New DataTable
cloneTable = table.Clone()
' Add column.
cloneTable.Columns.Add("extra", _
Type.GetType("System.String"))
' Add two rows. Note that the id column can't be the
' same as existing rows in the DataSet table.
Dim newRow As DataRow
newRow = cloneTable.NewRow()
newRow("id") = 12
newRow("Item") = 555
newRow("extra") = "extra Column 1"
cloneTable.Rows.Add(newRow)
newRow = cloneTable.NewRow()
newRow("id") = 13
newRow("Item") = 665
newRow("extra") = "extra Column 2"
cloneTable.Rows.Add(newRow)
' Merge the table into the DataSet.
Console.WriteLine("Merging")
dataSet.Merge(cloneTable, False, MissingSchemaAction.Add)
PrintValues(dataSet, "Merged With Table, Schema Added")
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
Dim row As DataRow
Dim column As DataColumn
For Each table In dataSet.Tables
Console.WriteLine("TableName: " & table.TableName)
For Each row In table.Rows
For Each column In table.Columns
Console.Write(ControlChars.Tab & " " _
& row(column).ToString())
Next column
Console.WriteLine()
Next row
Next table
End Sub