【问题标题】:Pass a variable using Script task in SSIS在 SSIS 中使用脚本任务传递变量
【发布时间】:2013-10-25 23:10:01
【问题描述】:

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

但我知道我错了……

【问题讨论】:

标签: ssis


【解决方案1】:

要在脚本中使用变量,首先确保该变量已添加到 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();

【讨论】:

    【解决方案2】:

    我是这样做的:

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

    在代码中你可以这样使用

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

    【讨论】:

      【解决方案3】:

      脚本任务中的以下代码可能会对您有所帮助

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-01-09
        • 2013-09-13
        • 1970-01-01
        • 2016-10-10
        • 1970-01-01
        • 2012-09-19
        • 1970-01-01
        相关资源
        最近更新 更多