1

我有一个 SQL DB,我使用 VB.Net 代码将数据导出为 XML。代码相对简单,运行速度快,并且可以漂亮地格式化 XML。代码是:

    Dim connetionString As String
    Dim connection As SqlConnection
    Dim adapter As SqlDataAdapter
    Dim ds As New DataSet
    Dim sql As String

    connetionString = "**connectionstring**"
    connection = New SqlConnection(connetionString)
    sql = "select * from costdata"
    Try
        connection.Open()
        adapter = New SqlDataAdapter(sql, connection)
        adapter.Fill(ds)
        connection.Close()
        ds.WriteXml("**PATH**")
        MsgBox("Done")
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

我遇到的问题是重新加载这些数据。看起来它应该像上面那样简单,但我似乎无法找到一种简单的方法来做到这一点。

据我了解,我可以将XMLReaderADO.NET结合使用,但在这种情况下,我需要为 DataTable 定义列,以便在将 XML 数据全部导入数据库之前将其插入。

有什么方法可以避免对 DataTable 中的列值进行硬编码,并以与上述类似的方式导入导出的 XML 数据?

4

2 回答 2

0

虽然它不是按列名自动执行的,但我认为对映射进行硬编码并不太麻烦。然而,我对自动化方式很感兴趣。我的解决方案:

    Dim connectionString As String = "Data Source=(localdb)\v11.0;Initial Catalog=localACETest;Integrated Security=True"
    Try
        Using sqlconn As New SqlConnection(connectionString)
            Dim ds As New DataSet()
            Dim sourcedata As New DataTable()
            ds.ReadXml("C:\Users\coopere.COOPERE-PC\Desktop\Test.xml")
            sourcedata = ds.Tables(0)
            sqlconn.Open()
            Using bulkcopy As New SqlBulkCopy(sqlconn)
                bulkcopy.DestinationTableName = "ScheduleData"
                bulkcopy.ColumnMappings.Add("Id", "Id")
                bulkcopy.ColumnMappings.Add("Period", "Period")
                ...

                bulkcopy.WriteToServer(sourcedata)
            End Using
            sqlconn.Close()
        End Using
        MsgBox("Done")
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
于 2013-07-16T22:43:09.070 回答
0

这是一种自动化列映射的方法……它假设表在目标数据库中存在相同的结构。干杯:-)

Public Shared Function BulkCopyXML( _
            path_ As String, _
            connection_string_ As String, _
            messages_ As List(Of String), _
            exceptions_ As List(Of Exception) _
    ) As Boolean
    Dim result_ As Boolean = False
    Try

        Dim dataset_ As New DataSet()
        dataset_.ReadXml(path_)
        Dim datatable_ As DataTable = Nothing

        Using connection_ As SqlClient.SqlConnection = New SqlClient.SqlConnection(connection_string_)
            connection_.Open()
            Using bulkCopy_ As SqlClient.SqlBulkCopy = New SqlClient.SqlBulkCopy(connection_)
                For Each datatable_ In dataset_.Tables()
                    messages_.Add(datatable_.TableName)
                    bulkCopy_.DestinationTableName = datatable_.TableName
                    bulkCopy_.ColumnMappings.Clear()
                    For Each dataColumn_ As DataColumn In datatable_.Columns
                        bulkCopy_.ColumnMappings.Add(dataColumn_.ColumnName, dataColumn_.ColumnName)
                    Next
                    bulkCopy_.WriteToServer(datatable_)
                Next
            End Using
        End Using

        result_ = True
    Catch exception_ As Exception
        If exceptions_ Is Nothing Then
            Throw exception_
        Else
            exceptions_.Add(exception_)
        End If
    Finally

    End Try
    Return result_
End Function
于 2013-11-08T00:13:19.620 回答