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:
virtual IEnumerator^ GetEnumerator() override

Return Value

Type: System.Collections::IEnumerator^

An IEnumerator object that represents the item collection.

Exception Condition
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.

No code example is currently available or this language may not be supported.

The procedure displays the following text in the Console window:

Peter
Mary
Russ

.NET Framework
Available since 2.0
Return to top
Show: