Question

I have a SQL DB from which I export data as XML using VB.Net code. The code is relatively simple, works quickly, and formats the XML beautifully. The code is:

    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

The problem I'm having is loading this data back in. It seems like it should be as easy as the above, but I can't seem to get a simple way to do it.

It's my understanding that I can use an XMLReader coupled with ADO.NET, but in that case I need to define the columns for the DataTable to insert the XML Data into before I import it all into the DB.

Is there any way to keep from having to hard-code column values in the DataTable, and have the exported XML data import in similar fashion to the above?

Was it helpful?

Solution

Though it's not automated by column name, I decided that hardcoding the mappings wasn't too big a hassle. I'm all ears for an automated way, however. My solution:

    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

OTHER TIPS

Here is a way to automate column mappings ... it assumes the table exists with the same structure in the target database. Cheers :-)

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top