在SSIS中从ADO Connection获取connectionstring的问题

时间:2015-12-15 22:40:40

标签: .net sql-server vb.net ssis

花了我几个小时来找出真正的问题。这是问题所在。

我在SSIS 2005包中有一个VB.Net脚本任务。脚本任务的目的是从一个数据库批量插入另一个数据库(在不同的服务器中)。我正在使用.Net中的SQlBulkCopy类进行插入。我使用Windows身份验证时工作正常。

现在我将其设为SQL Server Authentication并使用expressions从存储在配置文件中的变量值创建连接字符串。 SSIS能够成功获取ADO连接 - 但是当我从connection object中提取连接字符串时,它没有给出密码 - 它会吞下密码。

Dim destinationConnection As New SqlConnection()
destinationConnection = DirectCast(TryCast(Dts.Connections("ADOACIConnection").AcquireConnection(Dts.Transaction), SqlConnection), SqlConnection)

Dim destinationConnectionString As String = destinationConnection.ConnectionString
MessageBox.Show(destinationConnectionString)  'It is not getting password when using SQL Server Authentication
PerformBulkCopy(tableName, sourceConnection, destinationConnectionString)

问题

  1. 我们怎样才能获得带密码的连接字符串?
  2. CODE

    Public Sub PerformBulkCopy(ByVal tableName As String, ByVal sourceConnection As SqlConnection, ByVal destinationConnectionString As String)
        Dim dtSource As DataTable
        dtSource = New System.Data.DataTable()
        dtSource = GetSourceDataTable(tableName, sourceConnection)
    
    
        Using sbc As New SqlBulkCopy(destinationConnectionString)
            sbc.DestinationTableName = tableName
            sbc.WriteToServer(dtSource)
        End Using
    End Sub
    
    
    Public Function GetSourceDataTable(ByVal tableName As String, ByVal sourceConnection As SqlConnection) As DataTable
    
        Dim cmd As New SqlCommand("uspGetDataForAchival")
        cmd.Parameters.Add(New SqlParameter("@TableName", tableName))
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = sourceConnection
    
        Dim dap As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        dap.Fill(ds)
        Return ds.Tables(0)
    
    End Function
    
    Public Sub Main()
    
        Dim tableName As String = Dts.Variables("User::CurrentTable").Value.ToString()
        Dim sourceConnection As New SqlConnection()
    
        Try
    
            sourceConnection = DirectCast(TryCast(Dts.Connections("ADOSourceConnection").AcquireConnection(Dts.Transaction), SqlConnection), SqlConnection)
    
            Dim destinationConnection As New SqlConnection()
            destinationConnection = DirectCast(TryCast(Dts.Connections("ADOACIConnection").AcquireConnection(Dts.Transaction), SqlConnection), SqlConnection)
    
            Dim destinationConnectionString As String = destinationConnection.ConnectionString
            MessageBox.Show(destinationConnectionString)  
            PerformBulkCopy(tableName, sourceConnection, destinationConnectionString)
    
            Dts.TaskResult = Dts.Results.Success
    
        Catch ex As Exception
    
            Dim exceptionVariable As Microsoft.SqlServer.Dts.Runtime.Variables = Nothing
            Dts.VariableDispenser.LockOneForWrite("User::ScriptException", exceptionVariable)
            exceptionVariable("User::ScriptException").Value = ex.Message
            exceptionVariable.Unlock()
            Dts.Events.FireError(-1, "Task Name", ex.Message, String.Empty, 0)
            Dts.TaskResult = Dts.Results.Failure
        End Try
    
    End Sub
    

1 个答案:

答案 0 :(得分:1)

Thansk @billinkc为Todd McDermid关于Use Connections Properly in an SSIS Script Task

的博客文章添加了参考

我读过的另一篇文章是MSDN - SSIS Team Blog - Accessing OLEDB Connection Managers in a Script

最后,我修改了代码以解决问题,并根据上述博文发布连接。我只是传递连接对象来执行SqlBulkCopy,而不是从连接字符串创建连接。它运作良好。

    Public Sub PerformBulkCopy(ByVal tableName As String, ByVal sourceConnection As SqlConnection, ByVal destinationConnection As SqlConnection)
    Dim dtSource As DataTable
    dtSource = New System.Data.DataTable()
    dtSource = GetSourceDataTable(tableName, sourceConnection)


    Using sbc As New SqlBulkCopy(destinationConnection)
        sbc.DestinationTableName = tableName
        sbc.WriteToServer(dtSource)
    End Using
End Sub


Public Function GetSourceDataTable(ByVal tableName As String, ByVal sourceConnection As SqlConnection) As DataTable

    Dim cmd As New SqlCommand("uspGetDataForAchival")
    cmd.Parameters.Add(New SqlParameter("@TableName", tableName))
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Connection = sourceConnection

    Dim dap As New SqlDataAdapter(cmd)
    Dim ds As New DataSet()
    dap.Fill(ds)
    Return ds.Tables(0)

End Function

Public Sub Main()

    Dim tableName As String = Dts.Variables("User::CurrentTable").Value.ToString()
    Dim sourceConnection As SqlConnection
    Dim destinationConnection As SqlConnection

    Try
        ' Retrieve the reference to the managed connection'
        Dim cmSource As ConnectionManager
        cmSource = Dts.Connections("ADOSourceConnection")
        sourceConnection = DirectCast(cmSource.AcquireConnection(Dts.Transaction), System.Data.SqlClient.SqlConnection)

        Dim cmDestination As ConnectionManager
        cmDestination = Dts.Connections("ADOACIConnection")
        destinationConnection = DirectCast(cmDestination.AcquireConnection(Dts.Transaction), System.Data.SqlClient.SqlConnection)

        Dim destinationConnectionString As String = destinationConnection.ConnectionString
        MessageBox.Show(destinationConnectionString)
        PerformBulkCopy(tableName, sourceConnection, destinationConnection)

        'Release Connection'
        cmSource.ReleaseConnection(sourceConnection)
        cmDestination.ReleaseConnection(destinationConnection)

        'Success'
        Dts.TaskResult = Dts.Results.Success


    Catch ex As Exception

        Dim exceptionVariable As Microsoft.SqlServer.Dts.Runtime.Variables = Nothing
        Dts.VariableDispenser.LockOneForWrite("User::ScriptException", exceptionVariable)
        exceptionVariable("User::ScriptException").Value = ex.Message
        exceptionVariable.Unlock()
        Dts.Events.FireError(-1, "Task Name", ex.Message, String.Empty, 0)
        Dts.TaskResult = Dts.Results.Failure
     End Try

    End Sub