0

我在 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" +");"

但我知道我错了...

4

3 回答 3

2

要在脚本中使用变量,首先根据您的代码是否需要写入多变的。

//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();
于 2013-10-25T15:10:01.957 回答
1

我这样做:

打开脚本任务属性时,您有两个字段,ReadOnlyVariables并且ReadWriteVariables. 在您的情况下,根据您的需要将您的变量名称写入相应的字段User::Variable

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

Dts.Variables["User::Variable"].Value.ToString()
于 2013-05-22T11:21:06.160 回答
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);
于 2013-05-22T11:19:59.340 回答