共用方式為


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回 ,並傳回相同的物件,直到 MoveNext 呼叫 或 Reset 為止。 MoveNext 會將 Current 設定為下一個項目。

傳遞集合結尾之後,列舉值會放在集合的最後一個項目之後,而呼叫 MoveNext 會傳回 false。 如果最後一 MoveNext 次呼叫傳 false回 ,則呼叫 Current 會擲回例外狀況。 此外,由於提供 DataTableReader 其數據的順向存取權,因此呼叫 Reset IEnumerator 的 方法會 NotSupportedException擲回 。

提供 DataTableReader 穩定的列舉值。 這表示即使在基礎數據中發生數據列刪除或新增,呼叫 GetEnumerator 所傳回的枚舉器仍然有效。

適用於