Script Task: Picking up package connection

SSIS Script task within a package with a connection named Conn,
How do I pick up the name of the connection in Sub Main() of Class ScriptMain of the task?

I know there is a connection collection supporte by the DTS object, but what is the syntax for using it to pick up connection Conn?
hrj5721Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Megan BrooksSQL Server ConsultantCommented:
DTS.Connections is a collection of ConnectionManager objects, each of which has a Name property. Here is an example that enumerates the names of all the connection managers in the package:

Public Class ScriptMain
    Public Sub Main()
        Dim fireAgain As Boolean
        For Each cm As ConnectionManager In Dts.Connections
            Dts.Events.FireInformation(0, "", "CM: " & cm.Name, "", 0, fireAgain)
        Next
        Dts.TaskResult = Dts.Results.Success
    End Sub
End Class

You can reference the name of the first (and possibly only) connection manager of the package as DTS.Connections(0).Name.
0
hrj5721Author Commented:
I was able to reference the package included connection, as I verified by the Msgbox statement below.  However, I was not able to use it to connect to the database per the commented out Dim statement.  I was, however, to connect via the next statement that goes around the SSIS information about the existing connection for the package.

How can one connect using the DTS object, rather than going around it?

-----------------

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime

Public Class ScriptMain

    Public Sub Main()

        MsgBox(Dts.Connections(0).Name)

        Dim connection As SqlConnection
        ' Dim connectionString As String = Dts.Connections(0).Name

       Dim connectionString As String = "Data Source=FRY-GHX1WR0PE53\Medusa;Integrated Security=SSPI;Initial Catalog=CustMod4"
       
        ' MsgBox("connectionString")
        Dim queryString As String = "SELECT profID, bill_fname, bill_lname FROM dbo.local_CustomerStaging"
        connection = New SqlConnection(connectionString)
        ' connection.Open()
        ' connection = Dts.Connections(0).Name
        Dim command As SqlCommand = connection.CreateCommand()
        command.CommandText = queryString
        Try
            connection.Open()
            Dim dataReader As SqlDataReader = command.ExecuteReader()

            Do While dataReader.Read()
                MsgBox("  " & Convert.ToString(dataReader(0)) & "  " & Convert.ToString(dataReader(1)) & "  " & Convert.ToString(dataReader(2)))
            Loop
            dataReader.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Dts.TaskResult = Dts.Results.Success
    End Sub

End Class
0
Megan BrooksSQL Server ConsultantCommented:
You can obtain the connection string from the connection manager as the ConnectionString property: Dts.Connections(0).ConnectionString. This will work with Windows authentication, as shown, but the ConnectionString will not include the password if one is stored in the connection manager, and the password property of the CM is write only.

Here is some code that accesses the ConnectionString. It goes on to enumerate all of the readable properties of the connection manager.

    Public Sub Main()
        Dim fireAgain As Boolean
        Dim cm As ConnectionManager = Dts.Connections(0)
        Dts.Events.FireInformation(0, "", cm.ConnectionString, "", 0, fireAgain)
        For Each p As DtsProperty In Dts.Connections(0).Properties
            If p.Get Then
                Dts.Events.FireInformation(0, "", p.Name & " = " & p.GetValue(cm).ToString, "", 0, fireAgain)
            End If
        Next
        '
        Dts.TaskResult = Dts.Results.Success
    End Sub


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.