DataTableReader.GetEnumerator Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Returns an enumerator that can be used to iterate through the item collection.

Namespace:   System.Data
Assembly:  System.Data (in System.Data.dll)

public override IEnumerator GetEnumerator()

Return Value

Type: System.Collections.IEnumerator

An IEnumerator object that represents the item collection.

Exception Condition

An attempt was made to read or access a column in a closed DataTableReader.

Enumerators only allow for reading the data in the DataTableReader. Enumerators cannot be used to modify the underlying collection.

At first, the enumerator is positioned before the first element in the collection. At this position, calling Current throws an exception. Therefore, you must call MoveNext to advance the enumerator to the first element of the collection before reading the value of Current.

Current returns a DbDataRecord, and returns the same object until either MoveNext or Reset is called. MoveNext sets Current to the next element.

After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling MoveNext returns false. If the last call to MoveNext returned false, calling Current throws an exception. In addition, because the DataTableReader provides forward-only access to its data, calling the Reset method of the IEnumerator throws a NotSupportedException.

The DataTableReader provides a stable enumerator. This means that even if row deletions or additions occur within the underlying data, the enumerator returned by a call to GetEnumerator is still valid.

The following example demonstrates the use of the GetEnumerator method. This includes the behavior of the enumerator when rows are deleted from the underlying DataTable while the enumerator is active.

public static void Main()
        DataTable userTable = new DataTable("peopleTable");

        userTable.Columns.Add("Id", typeof(int));
        userTable.Columns.Add("Name", typeof(string));

        // Note that even if you create the DataTableReader
        // before adding the rows, the enumerator can still
        // visit all the rows.
        DataTableReader reader = userTable.CreateDataReader();
        userTable.Rows.Add(new object[] { 1, "Peter" });
        userTable.Rows.Add(new object[] { 2, "Mary" });
        userTable.Rows.Add(new object[] { 3, "Andy" });
        userTable.Rows.Add(new object[] { 4, "Russ" });

        IEnumerator enumerator = reader.GetEnumerator();
        // Keep track of whether the row to be deleted
        // has actually been deleted yet. This allows
        // this sample to demonstrate that the enumerator
        // is able to survive row deletion.
        bool isRowDeleted = false;
        while (enumerator.MoveNext())
            DbDataRecord dataRecord = (DbDataRecord)enumerator.Current;

            // While the enumerator is active, delete a row.
            // This doesn't affect the behavior of the enumerator.
            if (!isRowDeleted)
                isRowDeleted = true;
    catch (Exception ex)

The procedure displays the following text in the Console window:


.NET Framework
Available since 2.0
Return to top