| title | Coding a Custom Foreach Enumerator | |
|---|---|---|
| description | Coding a Custom Foreach Enumerator | |
| author | chugugrace | |
| ms.author | chugu | |
| ms.date | 03/06/2017 | |
| ms.service | sql | |
| ms.subservice | integration-services | |
| ms.topic | reference | |
| helpviewer_keywords |
|
[!INCLUDEsqlserver-ssis]
After you have created a class that inherits from the xref:Microsoft.SqlServer.Dts.Runtime.ForEachEnumerator base class, and applied the xref:Microsoft.SqlServer.Dts.Runtime.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 xref:Microsoft.SqlServer.Dts.Runtime.ForEachEnumerator.InitializeForEachEnumerator%2A 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 xref:Microsoft.SqlServer.Dts.Runtime.ForEachEnumerator.Validate%2A 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 xref:Microsoft.SqlServer.Dts.Runtime.Variable or xref:Microsoft.SqlServer.Dts.Runtime.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 xref:Microsoft.SqlServer.Dts.Runtime.ForEachEnumerator.Validate%2A 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;
} Private variableNameValue As String
Public Property VariableName() As String
Get
Return Me.variableNameValue
End Get
Set (ByVal Value As String)
Me.variableNameValue = value
End Set
End Property
Public Overrides Function Validate(ByVal connections As Connections, ByVal variableDispenser As VariableDispenser, ByVal infoEvents As IDTSInfoEvents, ByVal log As IDTSLogging) As DTSExecResult
If Not variableDispenser.Contains(Me.variableNameValue) Then
infoEvents.FireError(0, "MyEnumerator", "The Variable " + Me.variableNameValue + " does not exist in the collection.", "", 0)
Return DTSExecResult.Failure
End If
Return DTSExecResult.Success
End Function During execution, the xref:Microsoft.SqlServer.Dts.Runtime.ForEachLoop container calls the xref:Microsoft.SqlServer.Dts.Runtime.ForEachEnumerator.GetEnumerator%2A method of the custom enumerator. In this method, the enumerator creates and populates its collection of items, and then returns the collection. The xref:Microsoft.SqlServer.Dts.Runtime.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 xref:Microsoft.SqlServer.Dts.Runtime.ForEachEnumerator.GetEnumerator%2A 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;
} Public Overrides Function GetEnumerator() As Object
Dim numbers As ArrayList = New ArrayList()
Dim randomNumber As Random = New Random(DateTime.Now)
Dim x As Integer
For x = 0 To 100- 1 Step x + 1
numbers.Add(randomNumber.Next())
Next
Return numbers
End Function Creating a Custom Foreach Enumerator
Developing a User Interface for a Custom ForEach Enumerator