Export (0) Print
Expand All

DataTableReader.GetEnumerator Method

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.

Implements

IEnumerable.GetEnumerator()

ExceptionCondition
InvalidOperationException

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()
{
    try
    {
        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;
                userTable.Rows[2].Delete();
            }
            Console.WriteLine(dataRecord.GetString(1));
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
    Console.ReadLine();
}

The procedure displays the following text in the Console window:

Peter
Mary
Russ

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft