• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1764
  • Last Modified:

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?
0
hrj5721
Asked:
hrj5721
  • 2
1 Solution
 
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

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now