Foxtable(狐表)用户栏目专家坐堂 → [求助]导入去重


  共有1651人关注过本帖树形打印复制链接

主题:[求助]导入去重

帅哥哟,离线,有人找我吗?
zyl19810322
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:110 积分:1545 威望:0 精华:0 注册:2017/8/2 21:39:00
[求助]导入去重  发帖心情 Post By:2017/8/15 16:20:00 [只看该作者]

假设两个mdb文件,含有多个相同的表,如何用代码一键导入而不会重复?
谢谢老师

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

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By: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
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:110 积分:1545 威望:0 精华:0 注册:2017/8/2 21:39:00
  发帖心情 Post By:2017/8/15 16:57:00 [只看该作者]

收到,谢谢

 回到顶部