使用带有2个不同连接字符串的SqlBulkCopy

时间:2014-10-30 22:37:53

标签: sql-server vb.net

我有一个应用程序从一个服务器(服务器配置文件)获取信息并将其放入另一个服务器上的集中式数据库中。我从文本文件循环了几个服务器。我正在编写此应用程序以消除对链接服务器的需求。

我试图使用SqlBulkCopy来复制数据,但我遇到了问题。我已成功使用SqlBulkCopy与单个SQL连接,但当我尝试使用2个不同的SQL连接时,我遇到了问题。数据永远不会到达其目标数据库。我已经通过代码进行了调试,并且可以看到它到达try / catch但是看起来命令bulkcopy.WriteToServer(reader)它实际上并没有将任何数据写入SQL。

Dim connectionString As String = "Server= <ThatServer>; integrated security=true"
Dim connectionString2 As String = "Server= <MyServer>; Initial Catalog = <CentralizedDatabase>; integrated security=true"

    Using sourceConnection As SqlConnection = _
        New SqlConnection(connectionString)
        sourceConnection.Open()

        Dim commandSourceData As SqlCommand = New SqlCommand("<mySQLcommand>", sourceConnection)
        Dim reader As SqlDataReader = commandSourceData.ExecuteReader

        Using sourceConnection2 As SqlConnection = _
            New SqlConnection(connectionString2)
            sourceConnection2.Open()
            Using destinationConnection As SqlConnection = _
                New SqlConnection(connectionString2)

                Using bulkcopy As SqlBulkCopy = _
                    New SqlBulkCopy(destinationConnection)
                    bulkcopy.DestinationTableName = _
                        "<MyTableName>"

                    Try
                        bulkcopy.WriteToServer(reader)
                    Catch ex As Exception
                        Console.WriteLine(ex.Message)
                    Finally
                        reader.Close()
                    End Try

                End Using
            End Using
            sourceConnection2.Close()
        End Using
      sourceConnection.Close()
    End Using

谁能看到我做错了什么?提前谢谢!

**解决。这是我的代码:

Dim connectionString As String = "Server = <ThatServer>; Initial Catalog = <sourceDB>; integrated security = true"
Dim connectionString2 As String = "Server= <MyServer>; Initial Catalog = <destinationDB>; integrated security=true"

    Using sourceConnection As SqlConnection = _
        New SqlConnection(connectionString)

        Dim commandSourceData As SqlCommand = New SqlCommand(<TSQL>, sourceConnection)

        sourceConnection.Open()
        Dim reader As SqlDataReader = commandSourceData.ExecuteReader()

        Using destinationConnection As SqlConnection = _
            New SqlConnection(connectionString2)
            destinationConnection.Open()
            Using bulkCopy As SqlBulkCopy = _
                New SqlBulkCopy(destinationConnection)
                bulkCopy.ColumnMappings.Add("SourceColumn1", "DestinationColumn1")
                bulkCopy.ColumnMappings.Add("SourceColumn2", "DestinationColumn2")
                bulkCopy.ColumnMappings.Add("SourceColumn3", "DestinationColumn3")
                bulkCopy.ColumnMappings.Add("SourceColumn4", "DestinationColumn4")
                bulkCopy.ColumnMappings.Add("SourceColumn5", "DestinationColumn5")
                bulkCopy.ColumnMappings.Add("SourceColumn6", "DestinationColumn6")
                bulkCopy.ColumnMappings.Add("SourceColumn7", "DestinationColumn7")
                bulkCopy.DestinationTableName = "<destinationTable>"
                bulkCopy.WriteToServer(reader)
            End Using
            destinationConnection.Close()
        End Using
        reader.Close()
        sourceConnection.Close()
    End Using

0 个答案:

没有答案