DataTableReader.GetEnumerator Yöntem
Tanım
Öğe koleksiyonunu yinelemek için kullanılabilecek bir Numaralandırıcı döndürür.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
Döndürülenler
IEnumeratorÖğe koleksiyonunu temsil eden nesne.An IEnumerator object that represents the item collection.
Özel durumlar
Kapalı içindeki bir sütunu okuma veya erişme girişiminde bulunuldu DataTableReader .An attempt was made to read or access a column in a closed DataTableReader.
Örnekler
Aşağıdaki örnek yönteminin kullanımını gösterir GetEnumerator .The following example demonstrates the use of the GetEnumerator method. Bu, Numaralandırıcı etkinken satır silindiğinde sıralayıcının davranışını içerir 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
Yordamda konsol penceresinde aşağıdaki metin görüntülenir:The procedure displays the following text in the Console window:
Peter
Mary
Russ
Açıklamalar
Numaralandırıcılar yalnızca içindeki verileri okumaya izin verir DataTableReader .Enumerators only allow for reading the data in the DataTableReader. Numaralandırıcılar, temel alınan koleksiyonu değiştirmek için kullanılamaz.Enumerators cannot be used to modify the underlying collection.
İlk olarak, Numaralandırıcı koleksiyondaki ilk öğeden önce konumlandırılır.At first, the enumerator is positioned before the first element in the collection. Bu konumda, çağırma Current bir özel durum oluşturur.At this position, calling Current throws an exception. Bu nedenle, MoveNext
değerini okumadan önce sayacı koleksiyonun ilk öğesine ilerletmek için çağrısı yapmanız gerekir Current
.Therefore, you must call MoveNext
to advance the enumerator to the first element of the collection before reading the value of Current
.
Current
bir döndürür DbDataRecord ve ya da çağrılana kadar aynı nesneyi MoveNext döndürür Reset .Current
returns a DbDataRecord, and returns the same object until either MoveNext or Reset is called. MoveNext
bir Current
sonraki öğeye ayarlar.MoveNext
sets Current
to the next element.
Koleksiyonun sonuna geçtikten sonra, Numaralandırıcı koleksiyondaki son öğeden sonra konumlandırılır ve çağırma MoveNext
false döndürür.After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling MoveNext
returns false. Döndürülen son çağrı, MoveNext
false
çağırma Current
bir özel durum oluşturur.If the last call to MoveNext
returned false
, calling Current
throws an exception. Ayrıca, DataTableReader kendi verilerine yalnızca ileri doğru erişim sağladığından, Reset IEnumerator metodunu çağırmak bir oluşturur NotSupportedException .In addition, because the DataTableReader provides forward-only access to its data, calling the Reset method of the IEnumerator throws a NotSupportedException.
, DataTableReader Kararlı bir Numaralandırıcı sağlar.The DataTableReader provides a stable enumerator. Bu, temel alınan veriler içinde satır silme veya eklemeler gerçekleşse bile, çağrısı tarafından döndürülen Numaralandırıcı GetEnumerator hala geçerli olur.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.