SSIS通过脚本任务写入对象变量

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)

sor*_*ell 9

  1. 您需要创建包可以使用的变量.在VS2010中,您可以单击SSIS-> Variables菜单选项以打开Variables窗口.点击"添加新",然后添加您的列表.我将使用名称minList和maxList.他们的数据类型应该是'对象'.
  2. 在脚本任务中,您可以将这些对象实例化为列表.但首先,您需要访问它们.打开脚本任务,并将它们添加为ReadWriteVariables.在"选择变量"模式对话框中为每个选项添加复选标记.selectVar
  3. 现在您已将它们添加为ReadWriteVariables,单击"编辑脚本".添加System.Collections.Generic命名空间以使用List数据类型.现在,实例化列表.

    Dts.Variables["User::minList"].Value = new List<DateTime>(); Dts.Variables["User::minList"].Value = new List<DateTime>();

  4. 您可以通过执行以下操作为变量创建更易于管理的变量名称:

    List<DateTime> minDateList = (List<DateTime>)Dts.Variables["User::minList"].Value;

  5. 最后,您可以使用List的Add方法将这些值添加到列表对象中.我会将它们添加到您正在阅读的循环中reader.Read().

  6. 在Foreach循环编辑器中,您将选择Foreach From Variable Enumerator和一个列表变量. foreach循环