Solved

Script Task: Picking up package connection

Posted on 2007-03-27
3
1,755 Views
Last Modified: 2013-11-30
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
Comment
Question by:hrj5721
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Expert Comment

by:Megan Brooks
ID: 18804951
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
 

Author Comment

by:hrj5721
ID: 18820593
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
 
LVL 16

Accepted Solution

by:
Megan Brooks earned 250 total points
ID: 18820888
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question