将XML导入Access [英] Import XML into Access

查看:176
本文介绍了将XML导入Access的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我使用以下代码将XML文件导入到Access数据库中:

        ;    dbname ="c:\ PC Auditer.accdb"
           xmlFile = strFileName
          将Dim ds作为新数据集
         昏暗的流作为新System.IO.FileStream(strFileName,System.IO.FileMode.Open)读取
         ds.ReadXml(streamRead)

           streamRead.Close()

         如果ds.Tables.Count> 0然后
                strConnection ="Provider = Microsoft.ACE.OLEDB.12.0;数据源=". & dbname
              objConnection =新的OleDbConnection(strConnection)
               objConnection.Open()
              objAdapter =新的OleDbDataAdapter()
               objAdapter.ContinueUpdateOnError = True
             将cb用作新的OleDbCommandBuilder(objAdapter)
对于intI = 0到ds.Tables.Count-1
  如果ds.Tables(intI).Rows.Count> 0然后
              nbsp; b   cb.RefreshSchema()
             b ;   objAdapter.SelectCommand = New OleDbCommand("SELECT * FROM [" + ds.Tables(intI).TableName +]",objConnection)
        ;                objAdapter.Fill(ds,ds.Tables(intI).TableName.ToString)
         &bsp ;        objAdapter.UpdateCommand = cb.GetUpdateCommand()
             nbsp; b ;    objAdapter.Update(ds,ds.Tables(intI).TableName.ToString)
            ;   如果结束
               下一步
                objConnection.Close()
          如果If

,则导入可以正常工作,但是当下一个XML文件导入的记录较少时,数据库不会得到更新,并且旧"记录仍保留在数据库中.

有人能指出我正确的方向吗?

亲切的问候,
Jurgen

Hi all,

I use the following code to import a XML file into an Access database:

            dbname = "c:\PC Auditer.accdb"
            xmlFile = strFileName
            Dim ds As New DataSet
            Dim streamRead As New System.IO.FileStream(strFileName, System.IO.FileMode.Open)
            ds.ReadXml(streamRead)

            streamRead.Close()

            If ds.Tables.Count > 0 Then
                strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbname
                objConnection = New OleDbConnection(strConnection)
                objConnection.Open()
                objAdapter = New OleDbDataAdapter()
                objAdapter.ContinueUpdateOnError = True
                Dim cb As New OleDbCommandBuilder(objAdapter)
                For intI = 0 To ds.Tables.Count - 1
                    If ds.Tables(intI).Rows.Count > 0 Then
                        cb.RefreshSchema()
                        objAdapter.SelectCommand = New OleDbCommand("SELECT * FROM [" + ds.Tables(intI).TableName + "] ", objConnection)
                        objAdapter.Fill(ds, ds.Tables(intI).TableName.ToString)
                        objAdapter.UpdateCommand = cb.GetUpdateCommand()
                        objAdapter.Update(ds, ds.Tables(intI).TableName.ToString)
                    End If
                Next
                objConnection.Close()
            End If

The import works fine, but when a next XML file is imported with less records, the database don't get updated and the 'old' records remain in the database.

Can somebody point me to the right direction.

With kind regards,
Jurgen

推荐答案


尝试创建一种名称可以反映它们提供的严格定义功能的方法.

如何?
用以下方法编写:详细说明您要执行的操作.
听起来很乏味,是的.

用于读取并返回数据集的OpenXmlFile.
数据集中的ForEach表.
/>
   AddTableToDataBase
   UpdateDatabase和保存更改.
  
结束ForEach

You have too much stuff going on inside of the one method for YOUR eyes to decipher.
Try to create methods with names that reflect the tightly defined function they provide.

How?
Write out in detail what it is that you are trying to do.
Sounds tedious, yeah I know.

OpenXmlFile for reading and return a Dataset.
ForEach Table in the Dataset.

   AddTableToDataBase
   UpdateDatabase and Save changes.
  
End ForEach


这篇关于将XML导入Access的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆