2013-05-22 68 views
0

我在SSIS包一个C#脚本以下传递使用脚本任务的变量在SSIS

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

我需要通过数据库名称(数据库)变量里面提到...

可能像在脚本任务这个

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

,但我知道我错了......

+0

您不应该在连接字符串中同时包含“Integrated Security = true”和用户标识和密码,请选择其中一个。 – stuartd

+1

使用[连接字符串生成器类](http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx) – stuartd

回答

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); 
0

我不喜欢这样写道:

当打开你有两个字段,ReadOnlyVariablesReadWriteVariables脚本任务属性。根据您的需求,将您的变量名称写入相应的字段,在您的案例中User::Variable

在代码中,你可以使用它像这样

Dts.Variables["User::Variable"].Value.ToString() 
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();