从SSIS脚本任务中的List更新表的最佳方法是什么?
我们有一个共享的类库。我在脚本任务中使用了dll来完成大部分必要的工作。然后,dll方法返回一个List,其中包含与其运行的进程相关的数据。我把这份清单写在桌子上是我的工作。
我想我会循环遍历List中的每个项目并运行更新SQL语句。
为简洁起见,我没有粘贴SQL语句,但它实际上是使用MERGE的Upsert。
实际上,我希望有一种方法可以将List输出到执行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 = ""
}
}
答案 0 :(得分:2)
这里最简单的方法是在循环外创建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