简体   繁体   English

如何使用C#以编程方式创建SSIS Foreach ADO枚举器

[英]How to create an SSIS Foreach ADO Enumerator programmatically with C#

I'm trying to use C# to build my package, and add a Foreach ADO Enumerator task to an SSIS package programmatically. 我正在尝试使用C#构建程序包,并以编程方式将Foreach ADO Enumerator任务添加到SSIS程序包中。 However, I can't work out how to set the Source to my ADO Object and Map the ADO Object fields to the Variable Mappings. 但是,我不知道如何将Source设置为ADO Object并将ADO Object字段映射到Variable Mappings。

Edit: 编辑:

I have this so far. 到目前为止,我已经知道了。 It creates the task ok, and I'm adding the variables. 它将创建任务,然后添加变量。 So I think I just need to be able to set the ADO Object Source Variable, but can't work it out. 所以我想我只需要能够设置ADO Object Source Variable,但是不能解决这个问题。

Executables executables = pkg.Executables;
ForEachLoop forEachLoop = executables.Add("STOCK:ForEachLoop") as ForEachLoop;

forEachLoop.Name = group.ToString();
PrecedenceConstraint constraint = pkg.PrecedenceConstraints.Add(mainPipe, forEachLoop);

// Create a VariableMappings and VariableMapping objects.
ForEachVariableMappings forEachVariableMappings = forEachLoop.VariableMappings;
ForEachVariableMapping forEachVariableMapping = forEachVariableMappings.Add();

forEachVariableMapping.VariableName = @"User::ClientName";
forEachVariableMapping.ValueIndex = 0;


ForEachVariableMapping forEachVariableMapping2 = forEachVariableMappings.Add();

forEachVariableMapping2.VariableName = @"User::LinkedServer";
forEachVariableMapping2.ValueIndex = 1;


ForEachEnumeratorInfo forEachEnumeratorInfo = app.ForEachEnumeratorInfos["Foreach ADO Enumerator"];
ForEachEnumeratorHost forEachEnumeratorHost = forEachEnumeratorInfo.CreateNew();
forEachEnumeratorHost.CollectionEnumerator = true;

I think you are asking how you can set the "ADO object source variable" in the Foreach ADO Enumerator configuration. 我想您是在问如何在Foreach ADO枚举器配置中设置“ ADO对象源变量”。

You are pretty close. 你很亲密 The foreach loop container also behaves a little like a Task, in the sense that it hosts an InnerObject. 就其托管InnerObject而言,foreach循环容器的行为也有点类似于Task。 This InnerObject is set to the appropriate Enumerator as needed. 根据需要将此InnerObject设置为适当的Enumerator。

In your code, you have created the ForEachEnumeratorHost object. 在您的代码中,您已经创建了ForEachEnumeratorHost对象。 If you looked at the documentation, you can see that it has an InnerObject property. 如果查看文档,则可以看到它具有InnerObject属性。 You need to access the InnerObject and cast it to exact type of Enumerator you want. 您需要访问InnerObject并将其强制转换为所需的确切Enumerator类型。 In this case, you should cast it to an object of type ForEachADOEnumerator . 在这种情况下,应将其强制转换ForEachADOEnumerator类型的对象。 Once you cast it, the cast object can then expose to you the properties such as Type, DataObjectVariable which can be used to configure the Ado Enumerator. 转换后,转换对象可以向您提供诸如Type,DataObjectVariable之类的属性,这些属性可用于配置Ado枚举器。

Also, to access the ForEachADOEnumerator object, you need to add a reference to the Microsoft.SqlServer.ForEachADOEnumerator.dll 另外,要访问ForEachADOEnumerator对象,您需要添加对Microsoft.SqlServer.ForEachADOEnumerator.dll的引用

So just do this: 因此,只需执行以下操作:

// cast the inner object as ForEachADOEnumerator 
        ForEachADOEnumerator adoEnum = forEachEnumeratorHost.InnerObject as ForEachADOEnumerator;

        // then access the properties and set them
        // set the variable
        adoEnum.DataObjectVariable = "User::MyVar";

        // set the enumeration mode
        adoEnum.Type = ADOEnumerationType.EnumerateRowsInFirstTable;

        // finally...
        forEachLoop.ForEachEnumerator = forEachEnumeratorHost;

As a general guideline, I would encourage you to look at the msdn documentation for all these respective classes; 作为一般指导原则,我鼓励您查看所有这些相应类的msdn文档;该文档仅供参考。 make manual changes to the ssis package and observe the changes in the xml and tie them back to the different classes, properties and methods from msdn documentation. 对siss包进行手动更改,并观察xml中的更改,并将其绑定到msdn文档中不同的类,属性和方法。 Pretty soon, you will get a hang for how to develop packages programmatically. 很快,您将垂涎如何以编程方式开发软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM