vb.net 合并两个数据表的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10671746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:06:36  来源:igfitidea点击:

Best way to merge two Datatables

vb.netdatatable

提问by dr alialadin

I need to marge two datatables with condition. I have a datatable where the data comes from a local XML Database and another datatable where the data comes from a remote SQL Server.

我需要用条件对两个数据表进行边缘处理。我有一个数据表,其中数据来自本地 XML 数据库和另一个数据表,其中数据来自远程 SQL Server。

If any update made in the remote datatable I need to update/merge with the local datatable. Here is what I have so far:

如果在远程数据表中进行了任何更新,我需要更新/与本地数据表合并。这是我到目前为止所拥有的:

Public Sub MargeTwoTable()

    Dim SQL As String = ""
    Dim RemoteTable As New DataTable
    Dim LocalTable As DataTable
    Dim dal As New DalComon
    Dim yy As Integer = 0
    Dim UpdateDate As String
    Dim TableName As String = "V_Book_Price"


    LocalTable = LoadDataTable(TableName, True)
    UpdateDate = LocalTable.Compute("MAX(update_date)", Nothing)


    SQL = "select * from V_Book_Price where Update_Date > '" & UpdateDate & "'"
    RemoteTable = dal.GetDataSetBySQL(SQL).Tables(0)

    If RemoteTable.Rows.Count > 0 Then

        For i = 0 To RemoteTable.Rows.Count - 1
            Dim st As DataRow


            Dim mm() As DataRow = LocalTable.Select("ID = '" & RemoteTable.Rows(i).Item("ID") & "'")

            If mm.Length = 0 Then

                st = LocalTable.NewRow

                For yy = 0 To RemoteTable.Columns.Count - 1
                    st(yy) = RemoteTable.Rows(i)(yy)
                Next

                LocalTable.Rows.Add(st)

            Else
                st = mm(0)

                For yy = 0 To RemoteTable.Columns.Count - 1
                    If IsDate(RemoteTable.Rows(i)(yy)) Then
                        st(yy) = CDate(RemoteTable.Rows(i)(yy)).ToString("s")
                    Else
                        st(yy) = RemoteTable.Rows(i)(yy)
                    End If
                Next

                mm = Nothing
            End If
        Next

    End If

End Sub

In this code data comes from the remote database which updates a date getter then the local database . Both tables have "ID" as the primary key. The code is working well, but the problem is that when more than 1000 records are updated this function takes too long using loops.

在这段代码中,数据来自远程数据库,它更新日期获取器,然后是本地数据库。两个表都将“ID”作为主键。代码运行良好,但问题是当更新超过 1000 条记录时,此函数使用循环花费的时间太长。

回答by Steve

Not sure if can be applicable, but have you ever looked at the DataTable.LoadDataRow()method?.
It seems a good candidate to substitute all of you code above.

不确定是否适用,但您是否看过 DataTable.LoadDataRow()方法?。
替换上面的所有代码似乎是一个不错的选择。

Your code could be simplified to these lines

您的代码可以简化为这些行

Dim row as DataRow
For Each row in RemoteTable.Rows
    LocalTable.LoadDataRow(row.ItemArray, false)
Next

Another alternative could be the DataTable.Mergethat could cut your code to a single line

另一种选择可能是DataTable.Merge,它可以将您的代码切成一行

LocalTable.Merge(RemoteTable, False)

However, the real effectiveness of these two methods depends on the schema compatibility and from the presence of AutoNumber (identity) columns.

但是,这两种方法的真正有效性取决于模式兼容性和自动编号(标识)列的存在。