Pass a variable using Script task in SSIS

ssis

I have a C# script in the ssis package as mentioned below

SqlConnection importTab = new SqlConnection(@"Server=ServerName;  
Integrated Security=true;user=;pwd=;database=DBname");

I need to pass the database name (DBName) inside a variable…

May be like this

SqlConnection importTab = new SqlConnection(@"Server=ServerName;  
Integrated Security=true;user=;pwd=;database="+"User::Variable" +");"

But I know I am wrong…

Best Solution

To use a variable in a script, first ensure that the variable has been added to either the list contained in the ReadOnlyVariables property or the list contained in the ReadWriteVariables property of this script task, according to whether or not your code needs to write to the variable.

//Example of reading from a variable:
DateTime startTime = (DateTime) Dts.Variables["System::StartTime"].Value;

//Example of writing to a variable:
Dts.Variables["User::myStringVariable"].Value = "new value";

//Example of reading from a package parameter:
int batchId = (int) Dts.Variables["$Package::batchId"].Value;

//Example of reading from a project parameter:
int batchId = (int) Dts.Variables["$Project::batchId"].Value;

//Example of reading from a sensitive project parameter:
int batchId = (int) Dts.Variables["$Project::batchId"].GetSensitiveValue();
Related Question