One of the common questions that I see is, “How do I make a connection dynamic?” Well there are two different ways I typically see the need for dynamic connections. The first is for different environments and the second is to actually programmatically change the connection to load multiple files.
So the first one is rather simple and the best way to implement different connection properties per each environment is to use Configuration Files (I’m not covering the SQL 2012 options in this post). Typically what I recommend there is:
- Create Variables to store ServerName, DatabaseName, Username and Password
- Enable Configuration files for your package
- Map the Variables to the Configuration Files
First to setup this example I set up package that has one connection in the connection manager called DemoSQLConnection and one package called ConnDemo.
To create Variables first make sure you can see the variables pane, you can do that by right clicking on the background of the Control Flow Tab and selecting Variables.
Alternatively you can go to the Menu bar and select View Other Windows Variables
Once you can now see your variables window let’s now create Variables for our Connection.
- DemoSQLServer – Used to store the server name to connect to
- DemoSQLDatabase – Used to store the database name to connect to
- DemoSQLUserName – Used to store the User Name for the connection
- DemoSQLPassword – Used to store the Password for the connection
Make sure you always have your variables in the correct scope and have the proper data type, for this the scope should be at the package level and the type should be string. I have also put in some default values for the variables.
If you are using windows authentication for your connection you can skip 3 and 4. (Remember the Windows user running the package must have access to the database)
Now that I have created the Variables I can enable the Configuration Files and map the variables. To do this you can right click on the background and select Package configurations or go the Menu and select SSIS Package Configurations
Once selected a window should open and you want to select the checkbox for Enable package configurations and click Add
For this example I am going to add a XML Configuration File, name it democonfig and place it in the c: drive
Click next and then add your variables to the Configuration file. You might wonder why variables and not just put the connection string in the config, two reasons I do this. First is because sometimes you create different connection types to the same server such as an OLEDB and a SQL Destination and it’s easier to just map the variables to the connection strings and define once than it is to modify both connection strings. The second reason is if the password is in a variable you can encrypt that variable (I won’t be discussing how to do that in this post)
Click next and then Name the Configuration and click Finish.
Now one last step is to map the Variables to the connection string. I prefer to do this with a variable. Create a Variable called DemoSQLConnectionString and in the properties window change the Evaluate as Expression to true and click the icon in the Expression property.
Set the Expression to
“Data Source=”+ @[User::DemoSQLServer] +”;User ID=”+ @[User::DemoSQLUserName] +”;Provider=SQLNCLI10.1;Initial Catalog=” + @[User::DemoSQLDatabase] + “;Password=” + @[User::DemoSQLPassword]
And then press Evaluate Expression to test it.
One last step now and that is set connection string of the connection to the variable. To do this we do the following. We select the connection from the connection manager and edit the expression property of ConnectionString and set it to the @[User::DemoSQLConntionString] variable.
Now you can test this by creating a Data Flow and using this connection and if you want to test to make sure it’s all working correctly go and change the password or server in the configuration file and the connection should fail.
Now as for the second way of dynamically setting connections during run time. That will be part two of this as this has already got a little on the long side.