DataTable.Merge 方法 将指定的 DataTable 与当前的 DataTable 合并。Merge 方法用于合并架构大致相似的两个 DataTable 对象。合并在客户端应用程序上通常用于将数据源中最近的更改合并到现有的 DataTable 中。这使客户端应用程序能够拥有用数据源中的最新数据刷新的 DataTable。合并操作仅对原始表和要合并的表进行操作。子表不会受到影响或被包括在内。如果某个表具有一个或多个作为关系的一部分定义的子表,则需要分别合并每个子表。示例
下面的控制台应用程序演示了 Merge 方法的 missingSchemaAction 参数的行为。该示例创建同一个表的两个版本,并修改第二个版本的架构。代码随后尝试将第二个表合并到第一个表中。 注意:
此示例演示如何使用 Merge 的一个重载版本。有关其他可用示例的信息,请参见各个重载主题。
Private Sub DemonstrateMergeTable()
Dim table1 As New DataTable("Items") ' Add columns
Dim idColumn As New DataColumn("id", GetType(System.Int32))
Dim itemColumn As New DataColumn("item", GetType(System.Int32))
table1.Columns.Add(idColumn)
table1.Columns.Add(itemColumn) ' Set the primary key column.
table1.PrimaryKey = New DataColumn() {idColumn} ' Add RowChanged event handler for the table.
AddHandler table1.RowChanged, AddressOf Row_Changed ' Add some rows.
Dim row As DataRow
For i As Integer = 0 To 3
row = table1.NewRow()
row("id") = i
row("item") = i
table1.Rows.Add(row)
Next i ' Accept changes.
table1.AcceptChanges()
PrintValues(table1, "Original values") ' Create a second DataTable identical to the first.
Dim table2 As DataTable = table1.Clone() ' Add column to the second column, so that the
' schemas no longer match.
table2.Columns.Add("newColumn", GetType(System.String)) ' Add three rows. Note that the id column can't be the
' same as existing rows in the original table.
row = table2.NewRow()
row("id") = 14
row("item") = 774
row("newColumn") = "new column 1"
table2.Rows.Add(row) row = table2.NewRow()
row("id") = 12
row("item") = 555
row("newColumn") = "new column 2"
table2.Rows.Add(row) row = table2.NewRow()
row("id") = 13
row("item") = 665
row("newColumn") = "new column 3"
table2.Rows.Add(row) ' Merge table2 into the table1.
Console.WriteLine("Merging")
table1.Merge(table2, False, MissingSchemaAction.Add)
PrintValues(table1, "Merged With table1, Schema added")
End SubPrivate Sub Row_Changed(ByVal sender As Object, _
ByVal e As DataRowChangeEventArgs)
Console.WriteLine("Row changed {0}{1}{2}", _
e.Action, ControlChars.Tab, e.Row.ItemArray(0))
End SubPrivate Sub PrintValues(ByVal table As DataTable, _
ByVal label As String)
' Display the values in the supplied DataTable:
Console.WriteLine(label)
For Each row As DataRow In table.Rows
For Each col As DataColumn In table.Columns
Console.Write(ControlChars.Tab + " " + row(col).ToString())
Next col
Console.WriteLine()
Next row
End Sub
下面的控制台应用程序演示了 Merge 方法的 missingSchemaAction 参数的行为。该示例创建同一个表的两个版本,并修改第二个版本的架构。代码随后尝试将第二个表合并到第一个表中。 注意:
此示例演示如何使用 Merge 的一个重载版本。有关其他可用示例的信息,请参见各个重载主题。
Private Sub DemonstrateMergeTable()
Dim table1 As New DataTable("Items") ' Add columns
Dim idColumn As New DataColumn("id", GetType(System.Int32))
Dim itemColumn As New DataColumn("item", GetType(System.Int32))
table1.Columns.Add(idColumn)
table1.Columns.Add(itemColumn) ' Set the primary key column.
table1.PrimaryKey = New DataColumn() {idColumn} ' Add RowChanged event handler for the table.
AddHandler table1.RowChanged, AddressOf Row_Changed ' Add some rows.
Dim row As DataRow
For i As Integer = 0 To 3
row = table1.NewRow()
row("id") = i
row("item") = i
table1.Rows.Add(row)
Next i ' Accept changes.
table1.AcceptChanges()
PrintValues(table1, "Original values") ' Create a second DataTable identical to the first.
Dim table2 As DataTable = table1.Clone() ' Add column to the second column, so that the
' schemas no longer match.
table2.Columns.Add("newColumn", GetType(System.String)) ' Add three rows. Note that the id column can't be the
' same as existing rows in the original table.
row = table2.NewRow()
row("id") = 14
row("item") = 774
row("newColumn") = "new column 1"
table2.Rows.Add(row) row = table2.NewRow()
row("id") = 12
row("item") = 555
row("newColumn") = "new column 2"
table2.Rows.Add(row) row = table2.NewRow()
row("id") = 13
row("item") = 665
row("newColumn") = "new column 3"
table2.Rows.Add(row) ' Merge table2 into the table1.
Console.WriteLine("Merging")
table1.Merge(table2, False, MissingSchemaAction.Add)
PrintValues(table1, "Merged With table1, Schema added")
End SubPrivate Sub Row_Changed(ByVal sender As Object, _
ByVal e As DataRowChangeEventArgs)
Console.WriteLine("Row changed {0}{1}{2}", _
e.Action, ControlChars.Tab, e.Row.ItemArray(0))
End SubPrivate Sub PrintValues(ByVal table As DataTable, _
ByVal label As String)
' Display the values in the supplied DataTable:
Console.WriteLine(label)
For Each row As DataRow In table.Rows
For Each col As DataColumn In table.Columns
Console.Write(ControlChars.Tab + " " + row(col).ToString())
Next col
Console.WriteLine()
Next row
End Sub
要插入到数据库的话,循环插入DataTable.Rows.Add(dtl.Rows[0].ItemArray),然后DataAdapter.Update(DataTable);
如果数据流很大呢?循环是不是效率低点啊?同时还要考虑在合并datatable时如果2个datatable有重复行删掉的请况。
我想问的是有没有简单点的方法,不用循环