Data Warehousing and Data Science

29 July 2010

SSIS: Looping with ADO Enumerator

Filed under: SSIS — Vincent Rainardi @ 11:43 am

In SSIS, using the Foreach Loop Container we can loop around 7 different things: file, item, ADO, ADO.NET Schema Rowset, Variable, Nodelist and SMO.

  1. File: we loop around a collection of files in a folder
  2. Item: we loop around rows of data that we define ourselves (can be more than 1 column)
  3. ADO: we loop around rows of a existing table
  4. ADO.NET schema rowset: we loop around a collection of any of these: Catalogs, Character Sets, Check Constrains, Collations, Column Privileges, Columns, Columns Domain Usage, Constraint Column Usage, Constraint Table Usage, Foreign Keys, Indexes, Key Column Usage, Indexes, Primary Keys, Procedure Columns, Procedure Parameters, Procedures, Provider Types, Referential Constraints, Schemata, Statistics, Table Constraints, Table Privileges, Tables, Translations, Usage Privileges, View Column Usage, Views, View Table Usage, SQL Languages, DB Info Keywords, DB Info Literals, Check Constraints by Table, Table Statistics, Tables Info, and Trustees.
  5. Variable: we loop around the values of a variable, for example a DataTable where we loop around the rows.
  6. Nodelist: we loop around a set of XML nodes, which are the results of applying XPath expression to an XML file.
  7. SMO: we loop around SQL Server Management Objects, such as tables, users, statistics, jobs, traces, brokers.

In this article we will use ADO enumerator. We’ll create a package that:

  1. Create a DataSet
  2. Create a DataTable in the DataSet
  3. Populate the DataTable with 2 rows, row 1: file1.txt, row 2: file2.txt
  4. Assign that DataSet to variable Var1
  5. Have a FELC ADO Enumerator with source = Var1 to loop around the rows of the DataTable
  6. Set the Variable Mapping of this FELC to Var2
  7. Inside this FELC loop create a Script Task that checks if a file with file name = the value of Var2 exists or not.

The overall package looks like this:

True, we don’t need to use FELC to loop around the DataTable rows. We could do it in the first C# Script task, right after step 3 (populate the DataTable). The reasons of we might want to use FELC are a) to use SSIS functionality such as transaction and checkpoint, b) to reuse the variable that was transferred to the FELC in downstream task(s).

1-4 Create & Populate DataTable, assign to Var1

OK let’s start. First create a variable called Var1, of type object, scope = package:

Create the Script Task, and set Var1 as ReadWrite variable:

Edit Script, and create the DataSet and DataTable:

// Create the table
DataSet DS1 = new DataSet();
DataTable T1 = new DataTable();
DataColumn C1 = new DataColumn();
C1.DataType = Type.GetType(“System.String”);
C1.ColumnName = “Col1”;

Usually we populate the datatable from a database table, but in this instance I’m going to populate it from a file:

// Populate the table from the file
StreamReader sr1;
sr1 = new StreamReader(“C:\\Data\\FELC per row\\list.txt”);
String s1;
DataRow DR1;
while ((s1 = sr1.ReadLine()) != null)
DR1 = DS1.Tables[0].NewRow();
DR1[“Col1”] = s1;

The List.txt file only contains 2 lines:

After populating the DataSet, we assign it to Var1:

// Set Variable
Dts.Variables[“Var1”].Value = DS1;
Dts.TaskResult = (int)ScriptResults.Success;

5. Create the Foreach Loop Container

Have a FELC ADO Enumerator with source = Var1 to loop around the rows of the DataTable

Set the collection of the FELC to ADO enumerator, rows in the first table:

6. Set the Variable Mapping

Set the Variable Mapping of this FELC to Var2 (create Var2 first, scope = package, type = string):

This means that every row on the DataTable will be assigned to Var2. It’s the first column only that will be assigned (Index 0 = first column).

7. Add a Script Task inside the loop

Add script task inside the loop to process each row. Set Var 2 as ReadOnlyVariables because we want to use within the script.

Display the value of Var2 and check the existance of the files mentioned in Var2:

string Var2 = Dts.Variables[“Var2”].Value.ToString();
if (File.Exists(“C:\\Data\\FELC per row\\”+Var2))
System.Windows.Forms.MessageBox.Show(Var2 + ” exists”);
System.Windows.Forms.MessageBox.Show(Var2 + ” does not exist”);
Dts.TaskResult = (int)ScriptResults.Success;

Apologies about the indentation. Within {} the code should be indented, but WordPress wouldn’t let it. Unless I format it as “preformatted”. But using preformatted the font would appear very small.

Now if we run it, it will display the value of Var2 and whether that file exist or not:

Rainer Stropek, founder of software architects“, wrote a good article about Foreach ADO Enumerator in The Code Project.

1 Comment »

  1. […] – – Share this:TwitterFacebookLike this:LikeBe the first to like this […]

    Pingback by SSIS Foreach Loop Container using ADO.NET « berbagisolusi — 5 March 2012 @ 9:46 am | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at

%d bloggers like this: