Solved

Script Task: Picking up package connection

Posted on 2007-03-27
3
1,725 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 14

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 14

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

948 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