DataTableReader.GetEnumerator 方法

定义

返回一个可用于循环访问项集合的枚举数。

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 对象,它代表项集合。

例外

尝试读取或访问已关闭的 DataTableReader 中的列。

示例

下面的示例演示 GetEnumerator 方法的用法。 这包括在枚举器处于活动状态时从基础 DataTable 中删除行时枚举器的行为。

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

该过程在控制台窗口中显示以下文本:

Peter  
Mary  
Russ  

注解

枚举器仅允许读取 中的数据 DataTableReader。 枚举器不能用于修改基础集合。

首先,枚举器位于集合中的第一个元素之前。 在此位置,调用 Current 将引发异常。 因此,在读取 MoveNext 的值之前,必须调用 Current 将枚举器向前移动到集合的第一个元素。

Current返回 一个 DbDataRecord,并在调用 或 Reset 之前MoveNext返回相同的 对象。 MoveNextCurrent 设置为下一个元素。

传递集合末尾后,枚举器位于集合中最后一个元素之后,调用 MoveNext 将返回 false。 如果最后一次 MoveNext 调用返回 , false则调用 Current 将引发异常。 此外,由于 DataTableReader 提供对其数据的仅向前访问,因此调用 Reset IEnumerator 的 方法会 NotSupportedException引发 。

DataTableReader提供稳定的枚举器。 这意味着,即使删除或添加行发生在基础数据中,调用 GetEnumerator 返回的枚举器仍然有效。

适用于