DataTableReader.GetEnumerator Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
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
IEnumerator Obiekt reprezentujący kolekcję elementów.
Wyjątki
Podjęto próbę odczytu lub uzyskania dostępu do kolumny w zamkniętym DataTableReaderobiekcie .
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 modułu wyliczającego, 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ą być używane do modyfikowania podstawowej kolekcji.
Na początku moduł wyliczający jest umieszczony przed pierwszym elementem w kolekcji. Na tym stanowisku 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.
Current Metoda zwraca obiekt DbDataRecordi zwraca ten sam obiekt do momentu MoveNext wywołania metody lub Reset .
MoveNext ustawia Current wartość na następny element.
Po przekazaniu końca kolekcji moduł wyliczający jest umieszczony po ostatnim elemenie w kolekcji, a wywołanie MoveNext zwraca wartość false. Jeśli ostatnie wywołanie zwracane MoveNextfalse, wywołanie Current zgłasza wyjątek. Ponadto, ponieważ obiekt DataTableReader zapewnia dostęp tylko do przekazywania do danych, wywołując Reset metodę IEnumerator zgłasza błąd NotSupportedException.
Element DataTableReader zapewnia stabilny moduł wyliczający. Oznacza to, że nawet w przypadku usunięcia wierszy lub dodatków w danych bazowych moduł wyliczający zwrócony przez wywołanie GetEnumerator metody jest nadal prawidłowy.