以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  [求助]导入去重  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=105251)

--  作者:zyl19810322
--  发布时间:2017/8/15 16:20:00
--  [求助]导入去重
假设两个mdb文件,含有多个相同的表,如何用代码一键导入而不会重复?
谢谢老师

[此贴子已经被作者于2017/8/15 16:43:32编辑过]

--  作者:有点甜
--  发布时间:2017/8/15 16:47:00
--  

 合并数据,去重?

 

Dim dlg As  new OpenFileDialog
dlg.MultiSelect = True
dlg.Filter = "数据库|*.mdb"
If dlg.ShowDialog = DialogResult.OK Then
    For Each file As String In dlg.FileNames
        If Connections.Contains("测试") Then Connections.Delete("测试")
        Connections.Add("测试","Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & file & ";Persist Security Info=False")
        For Each tn As String In Connections("测试").GetTableNames
            If DataTables.Contains(tn) Then
                Dim cmd As New SQLCommand
                Dim dt As DataTable
                cmd.ConnectionName = "测试"
                cmd.CommandText = "SELECT * From {" & tn & "}"
                dt = cmd.ExecuteReader()
               
                For Each dr As DataRow In dt.DataRows
                    Dim filter As String = "1=1"
                    For Each dc As DataCol In dt.DataCols
                        If DataTables(tn).DataCols.contains(dc.name) Then
                            If dr.IsNull(dc.name) Then
                                filter &= " and " & dc.name & " Is null"
                            ElseIf dc.IsDate Then
                                filter &= " and " & dc.name & " = #" & dr(dc.name) & "#"
                            Else
                                filter &= " and " & dc.name & " = \'" & dr(dc.name) & "\'"
                            End If
                        End If
                    Next
                    If DataTables(tn).Find(filter) Is Nothing Then
                        output.show(filter)
                        Dim nr As DataRow = DataTables(tn).AddNew()
                        For Each dc As DataCol In dt.DataCols
                            If DataTables(tn).DataCols.contains(dc.name) Then
                                If dr.Isnull(dc.name) Then
                                    nr(dc.name) = Nothing
                                Else
                                    nr(dc.name) = dr(dc.name)
                                End If
                            End If
                        Next
                    End If
                Next
            End If
        Next
    Next
End If


--  作者:zyl19810322
--  发布时间:2017/8/15 16:57:00
--  
收到,谢谢