Sql*_*Guy 6 c# variables ssis object task
我有一些代码,我想最终得到2个列表.开始和结束.
它们包含月份的开始日期和月份的结束日期.
这两个列表我想放入一个对象变量,所以我可以在ssis中的foreachloop容器中使用该对象,并使用startofmonth和endofmonthdates(变量:min和max)遍历每一行 - 但我不知道如何
这是我的代码:
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;
Run Code Online (Sandbox Code Playgroud)

现在您已将它们添加为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和一个列表变量.

| 归档时间: |
|
| 查看次数: |
33889 次 |
| 最近记录: |