DataTableReader.GetEnumerator DataTableReader.GetEnumerator DataTableReader.GetEnumerator DataTableReader.GetEnumerator Method

定義

項目のコレクションを反復処理するために使用できる列挙子を返します。Returns an enumerator that can be used to iterate through the item collection.

public:
 override System::Collections::IEnumerator ^ GetEnumerator();
public override System.Collections.IEnumerator GetEnumerator ();
override this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Overrides Function GetEnumerator () As IEnumerator

戻り値

項目のコレクションを表す IEnumerator オブジェクト。An IEnumerator object that represents the item collection.

例外

閉じている DataTableReader の列を読み取るかアクセスしようとしました。An attempt was made to read or access a column in a closed DataTableReader.

GetEnumerator メソッドの使用例を次に示します。The following example demonstrates the use of the GetEnumerator method. これには、列挙子がアクティブなときに、基にDataTableなるから行が削除されるときの列挙子の動作が含まれます。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();
}
Sub Main()
   Try
      Dim userTable As New DataTable("peopleTable")
      userTable.Columns.Add("Id", GetType(Integer))
      userTable.Columns.Add("Name", GetType(String))

      ' Note that even if you create the DataTableReader
      ' before adding the rows, the enumerator can still
      ' visit all the rows.
      Dim reader As DataTableReader = userTable.CreateDataReader()
      userTable.Rows.Add(1, "Peter")
      userTable.Rows.Add(2, "Mary")
      userTable.Rows.Add(3, "Andy")
      userTable.Rows.Add(4, "Russ")

      Dim enumerator As IEnumerator = 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.
      Dim isRowDeleted As Boolean = False
      While (enumerator.MoveNext())

         Dim dataRecord As DbDataRecord = CType(enumerator.Current, _
             DbDataRecord)

         ' While the enumerator is active, delete a row.
         ' This doesn't affect the behavior of the enumerator.
         If Not isRowDeleted Then
            isRowDeleted = True
            userTable.Rows(2).Delete()
         End If
         Console.WriteLine(dataRecord.GetString(1))
      End While
   Catch ex As Exception

      Console.WriteLine(ex)
   End Try
   Console.ReadLine()
End Sub

この手順では、コンソールウィンドウに次のテキストが表示されます。The procedure displays the following text in the Console window:

Peter  
Mary  
Russ  

注釈

列挙子は、 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. この位置で Current を呼び出すと、例外がスローされます。At this position, calling Current throws an exception. そのため、MoveNext の値を読み取る前に、Current を呼び出して列挙子をコレクションの最初の要素に進める必要があります。Therefore, you must call MoveNext to advance the enumerator to the first element of the collection before reading the value of Current.

Currentを返しMoveNext Reset 、またはが呼び出されるまで同じオブジェクトを返します。 DbDataRecordCurrent returns a DbDataRecord, and returns the same object until either MoveNext or Reset is called. MoveNext は、Current を次の要素に設定します。MoveNext sets Current to the next element.

コレクションの末尾が渡されると、列挙子はコレクションの最後の要素の後に配置され、 MoveNextを呼び出すと false が返されます。After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling MoveNext returns false. 前回 MoveNext を呼び出して false が返された場合に、Current を呼び出すと例外がスローされます。If the last call to MoveNext returned false, calling Current throws an exception. また、はDataTableReaderデータへの順方向専用アクセスを提供するため、IEnumerator Resetのメソッドを呼び出すとNotSupportedException、がスローされます。In addition, because the DataTableReader provides forward-only access to its data, calling the Reset method of the IEnumerator throws a NotSupportedException.

DataTableReader 、安定した列挙子を提供します。The DataTableReader provides a stable enumerator. これは、基になるデータ内で行の削除または追加が行われた場合でもGetEnumerator 、の呼び出しによって返される列挙子が引き続き有効であることを意味します。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.

適用対象