DataTableReader.GetEnumerator Metoda

Definicja

Zwraca moduł wyliczający, który może służyć do iterowania po kolekcji elementów.

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

Zwraca

Obiekt IEnumerator reprezentujący kolekcję elementów.

Wyjątki

Podjęto próbę odczytu lub uzyskania dostępu do kolumny w zamkniętej DataTableReaderkolumnie .

Przykłady

W poniższym przykładzie pokazano użycie GetEnumerator metody . Obejmuje to zachowanie modułu wyliczającego, gdy wiersze są usuwane z bazowego DataTable , gdy moduł wyliczający jest aktywny.

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

Procedura wyświetla następujący tekst w oknie Konsola:

Peter  
Mary  
Russ  

Uwagi

Moduły wyliczające zezwalają tylko na odczytywanie danych w obiekcie DataTableReader. Moduły wyliczania nie mogą służyć do modyfikowania podstawowej kolekcji.

Na początku moduł wyliczający jest umieszczony przed pierwszym elementem w kolekcji. W tej pozycji wywołanie Current zgłasza wyjątek. W związku z tym należy wywołać metodę MoveNext , aby przejść do pierwszego elementu kolekcji przed odczytaniem wartości Current.

CurrentDbDataRecordMetoda zwraca obiekt i zwraca ten sam obiekt do momentu MoveNext wywołania metody lub Reset . MoveNext ustawia Current element na następny.

Po przekazaniu końca kolekcji moduł wyliczający jest umieszczony po ostatnim elemecie w kolekcji, a wywołanie MoveNext metody zwraca wartość false. Jeśli ostatnie wywołanie zwracane MoveNextfalse, wywołanie Current metody zgłasza wyjątek. Ponadto, ponieważ obiekt DataTableReader zapewnia dostęp tylko do przodu do danych, wywołanie Reset metody IEnumerator zgłasza NotSupportedExceptionbłąd .

Moduł DataTableReader udostępnia stabilny moduł wyliczający. Oznacza to, że nawet jeśli w danych bazowych wystąpią usunięcia lub dodania wierszy, moduł wyliczający zwracany przez wywołanie GetEnumerator metody jest nadal prawidłowy.

Dotyczy