Foxtable(狐表)用户栏目专家坐堂 → 怎么导入多个mdb中的多个表


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

主题:怎么导入多个mdb中的多个表

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


加好友 发短信
等级:超级版主 帖子:107521 积分:546890 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2020/3/19 15:02:00 [显示全部帖子]

Dim dlg As New OpenFileDialog
dlg.Filter = "Access文件|*.mdb"
dlg.MultiSelect = True '允许选择多个文件
If dlg.ShowDialog =DialogResult.OK Then
    For Each fl As String In dlg.FileNames
        Dim name As String
        Dim year As Integer
        For year = 2019 To 2019
            Dim month As Integer
            For month = 1 To 12
                name = year & format(month,"00") & "_Memory"
                If DataTables.Contains(name) Then
                    Dim mg As New Merger
                    mg.SourcePath = fl
                    mg.SourceTableName = name
                    mg.DataTableName = name
                    mg.Merge()
                Else
                    Dim im As New Importer
                    im.SourcePath = fl
                    im.SourceTableName = name
                    im.NewTableName  = name
                    im.Import
                End If
            Next
        Next
        
    Next
    
End If
[此贴子已经被作者于2020/3/19 15:35:00编辑过]

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


加好友 发短信
等级:超级版主 帖子:107521 积分:546890 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2020/3/19 15:35:00 [显示全部帖子]

Dim im As New Importer
im.SourcePath = fl
im.SourceTableName = name
im.NewTableName  = name
im.Import

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


加好友 发短信
等级:超级版主 帖子:107521 积分:546890 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2020/3/19 15:50:00 [显示全部帖子]

1、要导入的数据和已有的表结构不一致

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


加好友 发短信
等级:超级版主 帖子:107521 积分:546890 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2020/3/19 16:09:00 [显示全部帖子]

要导入的mdb数据库是不是没有这些表?msgbox(name = year & format(month,"00") & "_Memory")
或者是要导入的mdb表的结构和foxtable里的表结构不一致

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


加好友 发短信
等级:超级版主 帖子:107521 积分:546890 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2020/3/19 17:15:00 [显示全部帖子]

代码是遍历12个月的,但是数据库里很多都只有5~12月份的表格,导入不存在的表格当然有问题了。

Dim dlg As New OpenFileDialog
dlg.Filter = "Access文件|*.mdb"
dlg.MultiSelect = True '允许选择多个文件
Dim lst As List(Of String)
If dlg.ShowDialog =DialogResult.OK Then
    For Each fl As String In dlg.FileNames
        If Connections.Contains("test") Then Connections.Delete("test")
        Connections.Add("test","Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fl & ";Persist Security Info=False") '动态连接数据库
        lst = Connections("test").GetTableNames '获取数据源里所有的表名
        Dim name As String
        Dim year As Integer
        For year = 2019 To 2019
            Dim month As Integer
            For month = 1 To 12
                name = year & format(month,"00") & "_Memory"
                If lst.Contains(name ) Then '如果数据库包含这个表
                    If DataTables.Contains(name) Then
                        Dim mg As New Merger
                        mg.SourcePath = fl
                        mg.SourceTableName = name
                        mg.DataTableName = name
                        mg.Merge()
                    Else
                        Dim im As New Importer
                        im.SourcePath = fl
                        im.SourceTableName = name
                        im.NewTableName  = name
                        im.Import
                    End If
                End If
            Next
        Next
    Next
End If

 回到顶部