Solved

Script Task: Picking up package connection

Posted on 2007-03-27
3
1,714 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
  • 2
3 Comments
 
LVL 13

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 13

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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
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 to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now