0
votes

I am having an issue programmatically setting connection strings.

Here is an overview of my project. I am using 2012 SSIS and have two connections in my connection managers (Source and Destination and both are OLE DB connections). I have multiple packages I want to run. Each of these packages only contain Execute SQL tasks. Each task I have the SQL Statement connection type set to OLE DB, the connection is Destination, the SQL Source Type is a File Connection, and the File Connection is a file located on my C drive that is a script file that I generate in my program.

I have these packages deployed on my server which is a 2012 instance. The source and destination connections are also on my server but on a 2008 R2 instance.

I loop through N number of databases. In each loop I can set the Connection String and IntialCatalog properties by executing a SQL statement:

DECLARE @var sql_variant = N'Data Source=MyServerName\SQL2008R2;Initial Catalog=DatabaseName1;Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;'

EXEC [SSISDB].[catalog].[set_object_parameter_value] @object_type=20, @parameter_name=N'CM.Destination.ConnectionString', @object_name=N'MAC', @folder_name=N'MAC', @project_name=N'MAC', @value_type=V, @parameter_value=@var

This does work since I can open up the configure option on my project and see that the connection string and initial catalog values change while I am stepping through the program. (I didn't include the SQL for the initial catalog but it is similar to the connection string)

Now when I execute this through my program, I the following errors:

The connection "{9C2C8088-CE67-4D93-81B8-EC364D6E78D1}" is not found. This error is thrown by Connections collection when the specified connection element is not found.

Is there a way to update the package so the Execute SQL Tasks have an updated connection?

As a side note, if I open the packages in Visual Studio and modify the connections, the packages execute. It is when I try to programmatically change the connections and execute or if I try to execute the packages that are deployed on the server when it errors.

I did see this question and although it is similar, I think it varies enough to have a separate question. SSIS Connection not found in package. I have found a lot of great info here, just not what I am looking for yet.

Any help/advice is greatly appreciated!

Thanks

Mark

1
So just so I'm clear: you're running the same Execute SQL tasks and their statements against a set of databases?Kyle Hale
Yes. That is correct.user2460929
I should clarify that the contents of the SQL statement may be different based on which destination database I am pointing to. So my AddEmployee Execute SQL Task runs AddEmployee.sql but AddEmployee.sql is going to differ between company 1 and company 2.user2460929

1 Answers

0
votes

My suggestion would be to move your database looping and connection string setting inside your packages by taking advantage of SSIS's Foreach Loop container.

  1. Create a new string variable in your package. Call it "User::DestinationConnectionString".
  2. Right-click on your Destination connection and choose Properties.
  3. Create an Expression for your connection's ConnectionString property and set to your User:DestinationConnectionString variable.
  4. Put all of your connection strings in a table. Let's call it ConnectionStrings.
  5. Create an Execute SQL task at the beginning of your package. Let's call it "Get Connection Strings."
  6. Set the connection to where your ConnectionStrings table is.
  7. Write a query to retrieve the connection strings.
  8. Set the ResultSet to Full Result Set.
  9. Set your Result Set to a variable of type Object. Let's call it User::ConnectionStrings. (The Result Name should be 0.)
  10. Create a ForEach Loop Container. Name it "For Each Connection String."
  11. Set the Collection Enumerator to Foreach ADO ENumerator.
  12. Set the ADO Object Source variable to User::ConnectionStrings.
  13. In the Variable Mappings tab, map Index 0 to your User::DestinationConnectionString variable.
  14. Place your Execute SQL tasks inside your foreach loop.

Now when the package runs, it will loop through each connection string, assign it to the variable, which the connection uses to get its connection, which the SQL task then runs against.

If you don't know your database connection strings until runtime, you can just write them into the table then and then kick off your package.