代码之家  ›  专栏  ›  技术社区  ›  navig8tr

SSIS脚本任务:如何从列表更新表

  •  0
  • navig8tr  · 技术社区  · 6 年前

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

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

    我想我将遍历列表中的每个项目,并运行update 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 = ""
            }
        }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Joon    6 年前

    这里要做的最简单的事情是在循环之外创建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。值,并调用命令。ExecuteOnQuery