我在ssis包中有一个C#脚本,如下所述
SqlConnection importTab = new SqlConnection(@"Server=ServerName;
Integrated Security=true;user=;pwd=;database=DBname");
我需要在变量...
中传递数据库名称(DBName)可能是这样的
SqlConnection importTab = new SqlConnection(@"Server=ServerName;
Integrated Security=true;user=;pwd=;database="+"User::Variable" +");"
但我知道我错了......
答案 0 :(得分:2)
要在脚本中使用变量,请首先确保已将变量添加到 ReadOnlyVariables属性中包含的列表或包含在其中的列表 此脚本任务的ReadWriteVariables属性,根据您的是否 代码需要写入变量。
//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();
答案 1 :(得分:1)
我这样做:
打开脚本任务属性时,您有两个字段ReadOnlyVariables
和ReadWriteVariables
。根据您的需要,将您的变量名称写入相应字段User::Variable
。
在代码中你可以像这样使用它
Dts.Variables["User::Variable"].Value.ToString()
答案 2 :(得分:0)
脚本任务中的以下代码可以帮助您
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);