DataTableReader.GetEnumerator Metodo

Definizione

Restituisce un enumeratore che può essere utilizzato per scorrere la raccolta di elementi.

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

Valori restituiti

Oggetto IEnumerator che rappresenta l'insieme di elementi.

Eccezioni

È stato effettuato un tentativo di lettura o accesso a una colonna in un oggetto chiuso DataTableReader.

Esempio

Nell'esempio seguente viene illustrato l'uso del metodo GetEnumerator. Ciò include il comportamento dell'enumeratore quando le righe vengono eliminate dall'oggetto sottostante DataTable mentre l'enumeratore è attivo.

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

Nella finestra console viene visualizzato il testo seguente:

Peter
Mary
Russ

Commenti

Gli enumeratori consentono solo di leggere i dati in DataTableReader. Non è possibile utilizzare enumeratori per modificare la raccolta sottostante.

All'inizio, l'enumeratore viene posizionato prima del primo elemento della raccolta. In questa posizione, la chiamata Current genera un'eccezione. Pertanto, è necessario chiamare per far avanzare MoveNext l'enumeratore al primo elemento della raccolta prima di leggere il valore di Current.

Current restituisce un DbDataRecordoggetto e restituisce lo stesso oggetto finché non MoveNext viene chiamato o Reset . MoveNext imposta Current sull'elemento successivo.

Dopo aver passato la fine della raccolta, l'enumeratore viene posizionato dopo l'ultimo elemento della raccolta e la chiamata MoveNext restituisce false. Se l'ultima chiamata a MoveNext restituisce false, la chiamata Current genera un'eccezione. Inoltre, poiché fornisce l'accesso DataTableReader forward-only ai dati, la chiamata al Reset metodo di IEnumerator genera un'eccezione NotSupportedException.

fornisce DataTableReader un enumeratore stabile. Ciò significa che, anche se si verificano eliminazioni di righe o aggiunte all'interno dei dati sottostanti, l'enumeratore restituito da una chiamata a GetEnumerator è ancora valido.

Si applica a