SSIS foreach与简单对象的列表 [英] SSIS foreach with a list of simple objects

查看:213
本文介绍了SSIS foreach与简单对象的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

re: SSIS Foreach 使用变量循环任务:

我有这个工作原始对象列表(例如,字符串列表,如这个问题



但是,如何配置一个SSIS ForEach任务来循环 List< Dog> 或数组 Dog [] 其中 Dog 是一个简单的对象,如下所示:

  public class Dog {
public string Name {get; set;}
public string BestTrick {get;设置;}
}

可以说我试图得到名称 BestTrick 转换为循环内使用的两个字符串循环变量。



将变量映射到变量映射选项卡上的位置0和1似乎没有这样做。

解决方案

SSIS中的Foreach循环用于迭代对象列表。因此,对于上面的示例,您有一个一维列表, List< Dog> ,作为循环的输入。要获取列表中的当前项目,您需要指定变量映射到索引0.

如果您有一个二维列表,可以说列出< Dog,Owner> 然后你将通过映射一个变量到索引0来检索当前Dog对象,并且通过映射到索引1来获得当前的Owner。

在变量中包含当前对象后,可以通过将其转换为脚本任务中适当的类型来获取其属性。在脚本内部,你可以调用 dog.Name 并保存到另一个变量中,以便在其他组件中使用。

是脚本任务代码(C#)的一个例子,它从变量中获取狗对象,然后保存它的 Name BestTrick
$ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ )Dts.Variables [狗]价值;

Dts.Variables [DogName]。Value = d.Name;
Dts.Variables [DogTrick]。Value = d.BestTrick;




$ p
$ b

请注意你必须使每个变量都是你想要的编辑或读取其脚本任务已知的属性


re: SSIS Foreach Loop task with a Variable:

I have this working for a List of primitive objects (e.g. a list of strings, as discussed in this question)

But how do you configure an SSIS ForEach task to loop through a List<Dog> or array Dog[] where Dog is a simple object like this:

public class Dog {
    public string Name{ get; set;}
    public string BestTrick{ get; set;}
}

Lets say I'm trying to get Name and BestTrick into two string loop variables for use inside the loop.

Mapping variables to position 0 and 1 on the 'variable mappings' tab doesn't seem to do it.

解决方案

The Foreach Loop in SSIS is used for iterating over a list of objects. So, for the example above you have a one dimensional list, List<Dog>, as an input to the loop. To get the current item in the list you need to specify a variable mapping to index 0.

If you had a two dimensional list, lets say List<Dog,Owner> then you would retrieve the current Dog object by mapping a variable to index 0, and the current Owner by mapping to index 1.

Once you have the current object in a variable you can get its properties by casting it to the appropriate type in a Script Task. Inside the script you can call dog.Name and save it to another variable for use in other components.

This is an example of the Script Task code (C#) which retrieves the dog object from a variable, and then saves its Name and BestTrick to two different variables.

 public void Main()

       Dog d = (Dog) Dts.Variables["Dog"].Value;

       Dts.Variables["DogName"].Value = d.Name;
       Dts.Variables["DogTrick"].Value = d.BestTrick;

    }

Please note that you will have to make each variable you want to edit or read known to the Script Task in its properties

这篇关于SSIS foreach与简单对象的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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