我有一些代码,我想以 2 个列表结尾。开始和结束。
它们包含月份的开始日期和月份的结束日期。
我想将这 2 个列表放入一个对象变量中,这样我就可以在 ssis 的 foreachloop 容器中使用该对象,并使用 startofmonth 和 endofmonthdates(变量:最小值和最大值)遍历每一行 - 但我不知道如何
这是我的代码:
String s = "2013-01-01";
String b = "2014-01-01";
using (SqlConnection connection = new SqlConnection("Server=localhost;Initial Catalog=LegOgSpass;Integrated Security=SSPI;Application Name=SQLNCLI11.1"))
{
connection.Open();
string query = "select mindate,maxdate from dbo.dates";
using (SqlCommand command = new SqlCommand(query, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
s = reader.GetDateTime(0).ToShortDateString();
b = reader.GetDateTime(1).ToShortDateString();
//minDate.Add(reader.GetDateTime(0));
//maxDate.Add(reader.GetDateTime(1));
}
}
}
}
DateTime startdate = Convert.ToDateTime(s);
DateTime enddate = Convert.ToDateTime(b);
DateTime parseDate;
List<DateTime> minDate = new List<DateTime>();
List<DateTime> maxDate = new List<DateTime>();
List<DateTime> startings = new List<DateTime>();
List<DateTime> endings = new List<DateTime>();
startings.Add(startdate);
parseDate = startdate.AddMonths(1);
while (parseDate.Day != 1)
parseDate = parseDate.AddDays(-1);
parseDate = parseDate.AddDays(-1);
endings.Add(parseDate);
while (parseDate < enddate)
{
parseDate = parseDate.AddDays(1);
startings.Add(parseDate);
parseDate = parseDate.AddMonths(1);
parseDate = parseDate.AddDays(-1);
endings.Add(parseDate);
}
endings[endings.Count() - 1] = enddate;
for (var x = 0; x < startings.Count; x++)
{
Dts.Variables["test"].Value = x;
}
Dts.TaskResult = (int)ScriptResults.Success;
最佳答案
- 您需要创建一个包可以使用的变量。在VS2010中,可以点击SSIS->Variables菜单选项打开Variables窗口。单击“添加新”,然后添加您的列表。我将使用名称 minList 和 maxList。他们的数据类型应该是“对象”。
- 在您的脚本任务中,您可以将这些对象实例化为列表。但首先,您需要访问它们。打开您的脚本任务,并将它们添加为 ReadWriteVariables。在“选择变量”模式对话框中为每个添加复选标记。
既然您已将它们添加为 ReadWriteVariables,请单击“编辑脚本”。添加 System.Collections.Generic 命名空间以使用 List 数据类型。现在,实例化列表。
Dts.Variables["User::minList"].Value = new List<DateTime>();
Dts.Variables["User::minList"].Value = new List<DateTime>();
您可以通过执行以下操作为您的变量创建更易于管理的变量名称:
List<DateTime> minDateList = (List<DateTime>)Dts.Variables["User::minList"].Value;
最后,您可以使用 List 的 Add 方法将这些值添加到列表对象。我会将它们添加到您正在阅读的循环中
reader.Read()
.然后在您的 Foreach 循环编辑器中,您将选择 Foreach From Variable Enumerator 和您的列表变量之一。
关于c# - SSIS通过脚本任务写入对象变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28192083/