0

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...

2
  • You shouldn't have both Integrated Security=true AND a user ID and password in the connection string, choose one or the other. Commented May 22, 2013 at 11:18
  • 1
    Use the Connection String Builder class Commented May 22, 2013 at 11:20

3 Answers 3

2

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();
Sign up to request clarification or add additional context in comments.

Comments

1

I do it like this:

When opening the script task properties you have two fields, ReadOnlyVariables and ReadWriteVariables. Write your variable name into the according field based on your needs, in your case User::Variable.

In the code you can use it like this

Dts.Variables["User::Variable"].Value.ToString()

Comments

0

Following code in the Script task may help you

var dbServerName = Dts.Variables["yourVariableName"].Value.ToString();

var sqlConnString = string.Format("Server=ServerName;Integrated Security=true;user=;pwd=;database={0}", dbServerName);

SqlConnection sqlConn = new SqlConnection(sqlConnString);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.