Coding a Custom Foreach Enumerator


Applies To: SQL Server 2016 Preview

After you have created a class that inherits from the ForEachEnumerator base class, and applied the DtsForEachEnumeratorAttribute attribute to the class, you must override the implementation of the properties and methods of the base class to provide your custom functionality.

For a working sample of a custom enumerator, see Developing a User Interface for a Custom ForEach Enumerator.

You can override the InitializeForEachEnumerator method to cache references to the connection managers defined in the package, and to cache references to the events interface that you can use to raise errors, warnings, and informational messages.

You override the Validate method to verify that the enumerator is correctly configured. If the method returns Failure, the enumerator and the package that contains the enumerator will not be executed. The implementation of this method is specific to each enumerator, but if the enumerator relies on Variable or ConnectionManager objects, you should add code to verify that these objects exist in the collections that are provided to the method.

The following code example demonstrates an implementation of Validate that checks for a variable specified in a property of the enumerator.

private string variableNameValue;  
public string VariableName  
    get{ return this.variableNameValue; }  
    set{ this.variableNameValue = value; }  
public override DTSExecResult Validate(Connections connections, VariableDispenser variableDispenser, IDTSInfoEvents infoEvents, IDTSLogging log)  
    if (!variableDispenser.Contains(this.variableNameValue))  
        infoEvents.FireError(0, "MyEnumerator", "The Variable " + this.variableNameValue + " does not exist in the collection.", "", 0);  
            return DTSExecResult.Failure;  
    return DTSExecResult.Success;  

During execution, the ForEachLoop container calls the GetEnumerator method of the custom enumerator. In this method, the enumerator creates and populates its collection of items, and then returns the collection. The ForEachLoop then iterates the items in the collection, and executes its control flow for each item in the collection.

The following example shows an implementation of GetEnumerator that returns an array of random integers.

public override object GetEnumerator()  
    ArrayList numbers = new ArrayList();  
    Random randomNumber = new Random(DateTime.Now);  
    for( int x=0; x < 100; x++ )  
        numbers.Add( randomNumber.Next());  
    return numbers;  

Integration Services icon (small)  Stay Up to Date with Integration Services
For the latest downloads, articles, samples, and videos from Microsoft, as well as selected solutions from the community, visit the Integration Services page on MSDN:

 Visit the Integration Services page on MSDN

For automatic notification of these updates, subscribe to the RSS feeds available on the page.

Creating a Custom Foreach Enumerator
Developing a User Interface for a Custom ForEach Enumerator

Community Additions