I have a package that needs to be run in different environments (different server\instance, same database name) for example:
server: live-db01\live db: Campaign
server: dev-db01\livedebug db: Campaign
The package itself is identical for each environment. The only thing that changes is the connection string for the server. These packages are sent to appropriate department and they deploy it onto their server.
My question is let's say the package is installed onto the live-db01 Integration Services instance. Is there a way to access this server information inside the package and set the connection string accordingly?
Right now we deploy the same package with 4 different configurations with the only difference in the XML .config is the connection string. Is there a way to deploy a single package without the config that dynamically changes its connection string based on the server it is deployed in?
If you set the value of your connection string using a variable you can use a script task to set the variable to whatever you want.
Because script tasks have access to the .net base classes you can do a check against System.Environment.MachineName to get the host and set the config accordingly.
If you had a string variable called "CONNECTION_STRING" and added a script task that did something like:
string hostName = System.Environment.MachineName;
string connectionString = "";
switch (hostName)
{
case "host1":
connectionString = "SERVER=abc;Initial Catalog=blah;...";
break;
case "host2":
connectionString = "SERVER=abc;Initial Catalog=blah;...";
break;
case "host3":
connectionString = "SERVER=abc;Initial Catalog=blah;...";
break;
}
Dts.Variables["CONNECTION_STRING"].Value = connectionString;
That should do what you want. To use the expression go to the Expressions property on the connection and override the "ConnectionString" property with your variable "#[User::CONNECTION_STRING]" and you might want to enable delay validation for the connection.
Related
Setup
In the local environments, we're using SQL Authentication with Username and Password to connect to the databases. I created a Project Connection Manager that has expressions bound to Project Properties, Username and Password being set to sensitive.
On the dev server, when the SSIS run, it needs to use an AD account. I might need to create a Credential/Proxy for the SQL Agent, but for now I'm logged in as the user and I execute the packag through SQL.
Problem
In the SSIS project itself, I'm trying to configure a dynamic connection string to use Integrated Security in one case, and SQL Account in another. I just can't figure out how to do it. Things I tried:
1- Created a boolean "UseIntegratedSecurity" parameter. In the connection string, use that bool to set IntegratedSecurity=SSPI or not with the expression, and also use expressions to set the other attributes of the connection string individually. It didn't work, said the connection string could not be built.
2- Created a boolean "UseIntegratedSecurity" parameter, and write my connection string as something (ugly) like: #[$Project::IntegratedSecurity] ? "Data Source="+#[$Project::SqlServerName]+";Initial Catalog="+#[$Project::SqlServerDatabase]+";Provider=SQLNCLI11.1;Auto Translate=False;Integrated Security=SSPI;" : "Data Source="+#[$Project::SqlServerName]+";Initial Catalog="+#[$Project::SqlServerDatabase]+";Provider=SQLNCLI11.1;Auto Translate=False;User ID="+#[$Project::SqlServerUsername]+";Password=" + #[$Project::SqlServerPassword]
It didn't work because since SqlServerUserName and SqlServerPassword are sensitive, it refuses.
3- Tried having Project Parameters for ConnectionString, Server, Database, User, Password and setting them all. Works locally, but on the server, I get "Invalid Authorization Specifications".
Ideas?
Thanks
You need to handle situation when in one environment you have to use SQL Authentication, and on the other - AD Authentication.
This can be done with help of SSIS Catalog Environment variables. When you create a Project file, Visual Studio automatically creates the following so called project connection parameters for each OLEDB connection manager :
CM.< conn manager name >.ConnectionString
CM.< conn manager name >.InitialCatalog
CM.< conn manager name >.Password Created as sensitive param
CM.< conn manager name >.ServerName
CM.< conn manager name >.UserName
OLEDB is an example, SSIS creates similar parameters for other connection manager types.
Important fact, you do not have to create additional project parameters. The parameters mentioned are created on project being built and are present on all projects.
We create environment variables which specify connection string, DB name (initial catalog), Server Name etc. Good thing - Connection string variable is applied first, and then amended with the other variables.
More details on these parameters is in MS Docs.
In case similar to yours, in Dev environment - using SQL Auth define Conn string for SQL Auth and specify username and password in corresponding variables. In QA env where SSPI is used - the Connection string is reworked for SSPI, UserName and Password environment variables are empty.
I made EntityFramework (6) CodeFirst WinForms application
On my machine it works fine but when I'm trying to make Installation (with VS 2015 Installing projects) of my project and to run it on another machine, I get Expansion of Data Directory failed
I'm really dont understand- when I'm trying to change db name in ConnectionString to some wrong name- it still works.
My connection string in app config:
I found what was the problem. It is not enough to pass the name of connection string via Entity context constructor :base(ConnectionString). Entity context name MUST be the same as Connection string in app config. Other way EF create some another db.
I have a child package where the ConnectionString property of a Connection Manager is set by a Parent Package Variable Configuration. I set up a script task that brings up a message box with the value of the ConnectionString property right before the dataflow task.
`MessageBox.Show(Dts.Connections["CPU_*"].ConnectionString.ToString());`
When I run the parent package, the message box shows that the connection string is changing with every iteration, but in the dataflow it always draws the data from the same source.
I'm using SQL Server 2008 R2, the connection manager is an ADO.Net type, RetainSameConnection is set to False, and I've been researching this for days. Anybody have any ideas?
Update (2/23/2015): To make this stranger, when I look at the diagnostic logs, they tell me that when the new connections are being opened they are using the new connection strings.
I found an answer here Passing SSIS Connection String in parent variable works but package still validates against child design value.
I'm not sure about later versions yet, but certainly up to 2008R2 there is a bug when you pass a connection string into a child package. As you correctly point out the connection string IS passed, but either the connection is evaluated prior to the parent configuration or the connection string is updated from the design object after the parent configuration has been passed.
Either way it just doesn't work.
If, like me, you don't want to add an additional Script Task to all your packages you will need to do the following:
Parent Package
Lets assume we are using a OLE DB connection called MyDBConnection
Add a string variable to hold the connection string (MyConnection)
Add a C# Script Task (before the Package Task) with the line:
Dts.Variables["User::MyConnection"].Value = Dts.Connections["MyDBConnection"].ConnectionString;
Child Package(s)
Add a string variable to hold the connection string (MyConnection)
Add a parent package configuration to pass the value of MyConnection
In the properties for the OLE DB connection add an expression to update the Connection String property from MyConnection
When I create an SSIS Package Configuration settings, I have the option to export the connection managers and their properties. If I export all the settings, does that mean if I change the server on the "ConnectionString" I have to change it in the "InitialCatalog"? What happens if the ConnectionString property contains a different InitialCatalog than the "InitialCatalog" Property? Which one will SSIS use when I run the package?
conncetion string is the collection of
server name,
initial catalog(database name),
username,
password
you need to specify these and can change connection and if you want to change only single parameter then go with individual property.
If you had specified different values for a same property in same level then it will throws error.
you may not need to export ConnectionString property.
You can work with Initial Catalog and ServerName while using Native Security Mode.
And for that instance, where you want to keep Value of ConnectionString and Initial Catalog differently in separate variables that will throw an error.
The ConnectionString, Initail Catalog, ServerName, UserName and Password are given separately for different kind of ETL architecture and design.
When I create an SSIS Package Configuration settings, I have the option to export the connection managers and their properties. If I export all the settings, does that mean if I change the server on the "ConnectionString" I have to change it in the "InitialCatalog"? What happens if the ConnectionString property contains a different InitialCatalog than the "InitialCatalog" Property? Which one will SSIS use when I run the package?
I am using Entity Framework to access the data from my database. It's an MVC application and works fine locally. When I deploy the application on hosting (Parallels Plesk Panel, MS hosting) I get problems with accessing the SQL server instance. There are options in the cPanel which hold connection strings.
LocalSqlServer:
data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
xContainer:metadata=res:///Models.x.csdl|res:///Models.x.ssdl|res://*/Models.x.msl;provider=System.Data.SqlClient;provider connection string=
When I upload the site xContainer is generated alone. I found the sql server's instance name and applied it to the data source. In my web.config file I am using the the xContainer. The code after this paragraph is what it seems logic to me to add after the connection string= in the xContainer.
I have tried this with various properties. Data source, initial catalog, and the other info are filled into the conn string (here I am showing only /).
Data Source=x;Initial Catalog=/;Persist Security Info=True;User ID=/;Password=/;MultipleActiveResultSets=True providerName=
The error I receive is that the sql server instance cannot be found. If I add the last piece of code to the container it tells that I don't have a providerName, After adding a providerName the string is deleted to the starting xContainer string:
metadata=res:///Models.x.csdl|res:///Models.x.ssdl|res://*/Models.x.msl;provider=System.Data.SqlClient;provider connection string=
The error I receive is that the **sql server instance cannot be found**.
So, what is the SQL instance name? :)
it maybe not ".\SQLEXPRESS" but ".\SQLEXPRESS2012" or even ".\MSSQLSERVER" or anything else.
You will need to Edit the Web.Config file manually. The ASP.NET Settings page will remove the providerName.
An example of a connection string using EntityClient is below. You can remove the metadata information if you're not using an Entity Model. You will notice the providerName is outside of the actual connectionString and is the reason you will need to edit the file manually.
connectionString="metadata=ModelInformation;provider=System.Data.SqlClient;provider connection string="data source=IP;initial catalog=DATABASE;User ID=USERNAME;Password=PASSWORD;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient"