SSIS通过写脚本任务对象变量 [英] SSIS Write to object variable through script task

查看:152
本文介绍了SSIS通过写脚本任务对象变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,我想用2列出告终。 Startings和结局。



它们含有开始一个月,一个月的结束日期的日期。



这2列出我想要放在一个对象变量,所以我可以通过与startofmonth和endofmonthdates每行使用在SSIS中foreachloop容器中的对象,循环(变量:最小值和最大值) - 但我不知道如何



下面是我的代码:

 一个String =2013-01-01; 
字符串B =2014年1月1日;
{
连接使用(;初始目录= LegOgSpass;集成安全性= SSPI应用程序名称= SQLNCLI11.1服务器=本地主机)的SqlConnection连接=新的SqlConnection()

。打开();
查询字符串=选择MINDATE,MAXDATE从dbo.dates使用
(的SqlCommand命令=新的SqlCommand(查询,连接))
{使用
(SqlDataReader的读卡器= Command.ExecuteReader却())
{
,而(阅读器。阅读())
{
S = reader.GetDateTime(0).ToShortDateString();
B = reader.GetDateTime(1).ToShortDateString();

//minDate.Add(reader.GetDateTime(0));
//maxDate.Add(reader.GetDateTime(1));
}
}
}
}

DateTime的开始日期= Convert.ToDateTime(S);
DateTime的结束日期= Convert.ToDateTime(B);
的DateTime parseDate;

名单,LT; DateTime的> MINDATE =新的List<&日期时间GT;();
名单,LT; DateTime的> MAXDATE =新的List<&日期时间GT;();

名单,LT; DateTime的> startings =新的List<&日期时间GT;();
名单,LT; DateTime的>结局=新的List<&日期时间GT;();


startings.Add(开始日期);
parseDate = startdate.AddMonths(1);

,而(parseDate.Day = 1!)
parseDate = parseDate.AddDays(-1);
parseDate = parseDate.AddDays(-1);


endings.Add(parseDate);
,而(parseDate<结束日期)
{
parseDate = parseDate.AddDays(1);


startings.Add(parseDate);
parseDate = parseDate.AddMonths(1);
parseDate = parseDate.AddDays(-1);

endings.Add(parseDate);

}
结局[endings.Count() - 1] =结束日期;


为(VAR X = 0; X< startings.Count; X ++)
{
Dts.Variables [测试]值= X。
}


Dts.TaskResult =(INT)ScriptResults.Success;


解决方案

  1. 您需要创建一个变量,该包可以使用。在VS2010,您可以点击SSIS->变量菜单选项,打开变量窗口。点击添加新建,然后添加你的列表。我将使用名称minList和maxList。它们的数据类型应该是'对象。

  2. 在你的脚本任务,你可以举例说明这些对象的列表。但首先,你需要访问他们。打开你的脚本任务,并将其添加为ReadWriteVariables。复选标记添加到每个在选择变量模态对话框。

  3. 现在,您已经添加他们作为ReadWriteVariables单击编辑脚本。添加System.Collections.Generic命名空间使用List数据类型。现在,实例化列表



    Dts.Variables [用户:: minList]值=新的List< DateTime的>();
    Dts.Variables [用户:: minList]值=新的List< DateTime的>();


  4. 您可以通过以下操作为您的变量更容易管理的变量名:



    名单,LT ; DateTime的> minDateList =(列表< DateTime的>)Dts.Variables [用户:: minList]值;


  5. 最后,可以将这些值添加到使用列表的Add方法列表对象。我想补充他们你在哪里 reader.Read阅读循环中()


  6. 在你的Foreach循环编辑器,你会再选择在Foreach从变量枚举器,以及您的列表变量之一。



I have some code where i wanna end up with 2 lists. Startings and endings.

They contain start date of month and enddate of month.

These 2 lists i wanna put in an object variable so i can use the object in a foreachloop container in ssis,and loop through each row with startofmonth and endofmonthdates (variables: min and max) - But i dont know how to

Here are my codes:

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;

解决方案

  1. You need to create a variable that the package can use. In VS2010, you can click on the SSIS->Variables menu option to open the Variables window. Click 'Add New', and add your lists. I'll use the names minList and maxList. Their data types should be 'Object.'
  2. In your script task, you could instantiate these objects as Lists. But first, you need access to them. Open your script task, and add them as ReadWriteVariables. Add checkmarks to each in the Select Variables modal dialog.
  3. Now that you've added them as ReadWriteVariables, click Edit Script. Add the System.Collections.Generic namespace to use the List data type. Now, instantiate the Lists.

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

  4. You can create more manageable variable names for your variables by doing the following:

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

  5. Finally, you could add these values to the list objects using List's Add method. I would add them inside of the loop where you are reading from reader.Read().

  6. In your Foreach Loop Editor, you would then select the Foreach From Variable Enumerator, and one of your list variables.

这篇关于SSIS通过写脚本任务对象变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆