【问题标题】:SSIS Script Task: How to update table from List<T>SSIS 脚本任务:如何从 List<T> 更新表
【发布时间】:2018-04-16 20:50:17
【问题描述】:

从 SSIS 脚本任务中的列表更新表的最佳方法是什么?

我们有一个共享的类库。我在脚本任务中使用了 dll 来完成大部分必要的工作。 dll 方法然后返回一个列表,其中包含与其运行的进程相关的数据。将此列表写入表格是我的工作。

我想我将遍历列表中的每个项目并运行更新 SQL 语句。

为简洁起见,我没有粘贴 SQL 语句,但它实际上是一个使用 MERGE 的 Upsert。

实际上,我希望有一种方法可以将列表输出到执行 SQL 任务的输入,但我不确定这是否可能。

这是我目前所拥有的。如您所见,它尚未完成。

  private void UpdateEtlData(List<ProcessStatitics> statistics)
    {
        var connection = GetOhioConnectionString();

        // will loop thru each item in statistics and run the 
        // following sequence. This code is unfinished, but
        // I will use properties inside each statistic to form the 
        // query
        foreach(statistic in statistics)
        {
            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = ""
        }
    }

【问题讨论】:

  • 列表从何而来?
  • 需要更多细节
  • @Nick.McDermaid:该列表来自自定义 dll。 dll 是一个类库,它完成了大部分工作,然后以列表的形式写回过程中的一些细节。将此列表写入表格是我的工作。
  • @paparazzo:我添加了一些细节。
  • 您是否设法将 DLL 提供的列表传递给 SSIS 脚本任务?我认为这将是你最大的挑战。 SSIS 可能不是最好的工具。是否需要将参数传递到自定义 dll 中?

标签: sql-server tsql ssis


【解决方案1】:

您在这里做的最简单的事情是在循环之外创建您的 SQLCommand,并使用参数对其进行设置以写入您的数据。这篇博文很好地涵盖了它:http://csharp-station.com/Tutorial/AdoDotNet/Lesson06

步骤是:

// 1. declare command object with parameter
    SqlCommand cmd = new SqlCommand(
        "Insert into CityList (CityName) values (@City)", conn);
// 2. define parameters used in command object
    SqlParameter param  = new SqlParameter();
    param.ParameterName = "@City";
// 3. add new parameter to command object
    cmd.Parameters.Add(param);

// When you want to assign the value
    param.Value = inputCity;

然后在您的循环中,您可以将列表中的值分配给 param.Value,然后调用 command.ExecuteNonQuery

【讨论】:

    猜你喜欢
    • 2015-05-03
    • 1970-01-01
    • 2012-04-05
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    相关资源
    最近更新 更多