Dynamic connection string in SSIS

This is a common problem: Use a dtsx package with dynamic database connection.

In fact there are times that you need to set connection string of database from outside of package.


Suppose that you have an OLE DB connection and set it to same database which you want to work with and named it "Connection ".

1-Use a Script Task

2-Create four variables for your connection string details

3-pass them as ReadOnlyVariables to Script Task

4- write code below on your main method:

Public Sub Main()

        Dim oledbConnectionManager As ConnectionManager
        oledbConnectionManager = Dts.Connections("Connection")

        Dim oServerName As String
        Dim oUserName As String
        Dim oInitialCatalog As String
        Dim oPassword As String

        oServerName = Dts.Variables("User::ServerName").Value.ToString()
        oInitialCatalog = Dts.Variables("User::DatabaseName").Value.ToString()
        oUserName = Dts.Variables("User::UID").Value.ToString()
        oPassword = Dts.Variables("User::Pass").Value.ToString()

        Dts.Connections("Connection").Properties("ServerName").SetValue(oledbConnectionManager, oServerName)
        Dts.Connections("Connection").Properties("InitialCatalog").SetValue(oledbConnectionManager, oInitialCatalog)
        Dts.Connections("Connection").Properties("UserName").SetValue(oledbConnectionManager, oUserName)
        Dts.Connections("Connection").Properties("Password").SetValue(oledbConnectionManager, oPassword)

        Dts.TaskResult = ScriptResults.Success
    End Sub

Reza Rad on FacebookReza Rad on LinkedinReza Rad on TwitterReza Rad on Youtube
Reza Rad
Trainer, Consultant, Mentor
Reza Rad is a Microsoft Regional Director, an Author, Trainer, Speaker and Consultant. He has a BSc in Computer engineering; he has more than 20 years’ experience in data analysis, BI, databases, programming, and development mostly on Microsoft technologies. He is a Microsoft Data Platform MVP for 12 continuous years (from 2011 till now) for his dedication in Microsoft BI. Reza is an active blogger and co-founder of RADACAD. Reza is also co-founder and co-organizer of Difinity conference in New Zealand, Power BI Summit, and Data Insight Summit.
Reza is author of more than 14 books on Microsoft Business Intelligence, most of these books are published under Power BI category. Among these are books such as Power BI DAX Simplified, Pro Power BI Architecture, Power BI from Rookie to Rock Star, Power Query books series, Row-Level Security in Power BI and etc.
He is an International Speaker in Microsoft Ignite, Microsoft Business Applications Summit, Data Insight Summit, PASS Summit, SQL Saturday and SQL user groups. And He is a Microsoft Certified Trainer.
Reza’s passion is to help you find the best data solution, he is Data enthusiast.
His articles on different aspects of technologies, especially on MS BI, can be found on his blog: https://radacad.com/blog.

Leave a Reply