語言

IDataReader 介面

定義

提供讀取一或多個結果集的正向數據流的方法,這些結果集是藉由在數據源上執行命令所取得,並由存取關係資料庫的 .NET 數據提供者實作。

public interface class IDataReader : IDisposable, System::Data::IDataRecord
public interface IDataReader : IDisposable, System.Data.IDataRecord
type IDataReader = interface
    interface IDataRecord
    interface IDisposable
type IDataReader = interface
    interface IDisposable
    interface IDataRecord
Public Interface IDataReader
Implements IDataRecord, IDisposable
衍生
實作

範例

以下範例建立了 SqlConnection導出類別 、 、 SqlCommand和 SqlDataReader的實例。 此範例會讀取數據,並將其寫出至主控台。 最後,範例會封 SqlDataReader閉 ,接著 SqlConnection。

private static void ReadOrderData(string connectionString)
{
    string queryString =
        "SELECT OrderID, CustomerID FROM dbo.Orders;";

    using (SqlConnection connection =
               new SqlConnection(connectionString))
    {
        SqlCommand command =
            new SqlCommand(queryString, connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        // Call Read before accessing data.
        while (reader.Read())
        {
            ReadSingleRow((IDataRecord)reader);
        }

        // Call Close when done reading.
        reader.Close();
    }
}

private static void ReadSingleRow(IDataRecord dataRecord)
{
    Console.WriteLine(String.Format("{0}, {1}", dataRecord[0], dataRecord[1]));
}
Private Sub ReadOrderData(ByVal connectionString As String)
    Dim queryString As String = _
        "SELECT OrderID, CustomerID FROM dbo.Orders;"

    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)
        connection.Open()

        Dim reader As SqlDataReader = command.ExecuteReader()

        ' Call Read before accessing data.
        While reader.Read()
            ReadSingleRow(CType(reader, IDataRecord))
        End While

        ' Call Close when done reading.
        reader.Close()
    End Using
End Sub

Private Sub ReadSingleRow(ByVal record As IDataRecord)
   Console.WriteLine(String.Format("{0}, {1}", record(0), record(1)))
End Sub

備註

IDataReader與IDataRecord介面允許繼承類別實作一個DataReader類別,提供讀取一個或多個僅向前向的結果集串流。 欲了解更多類別 DataReader 資訊,請參閱 使用 DataReader 檢索資料。

應用程式不會直接建立介面的實例 IDataReader ,而是建立一個繼承 的 IDataReader類別實例。

繼承 IDataReader 類別必須實作繼承的成員,並通常定義額外成員以增加提供者專屬功能。

在資料讀取過程中,其他程序或執行緒對結果集所做的變更,可能對實 IDataReader作 的類別使用者可見。 然而,具體行為取決於提供者和時間。

給實施者的注意事項

為了促進.NET Framework 資料提供者間的一致性,將繼承類別命名為 Prv 指令,其中 Prv 是特定 .NET Framework 資料提供者命名空間中所有類別的統一前綴。 例如,Sql是命名空間中SqlDataAdapter類別的前綴System.Data.SqlClient。

使用者不會直接建立類別的 DataReader 實例。 相反地,他們是透過DataReader物件的方法ExecuteReader取得實Command例。 因此,你應該將建構器標記 DataReader 為內部。

屬性

名稱 Description
Depth

取得值,指出目前數據列的巢狀深度。

FieldCount

取得目前數據列中的數據行數目。

(繼承來源 IDataRecord)
IsClosed

會取得一個值,表示資料讀取器是否已關閉。

Item[Int32]

取得位於指定索引的欄位。

(繼承來源 IDataRecord)
Item[String]

取得指定名稱的欄位。

(繼承來源 IDataRecord)
RecordsAffected

取得執行 SQL 陳述句時所變更、插入或刪除的列數。

方法

名稱 Description
Close()

關閉物件 IDataReader 。

Dispose()

執行與釋放、釋放或重設非受控資源相關聯的應用程式定義工作。

(繼承來源 IDisposable)
GetBoolean(Int32)

取得指定之數據行的值做為布爾值。

(繼承來源 IDataRecord)
GetByte(Int32)

取得指定欄位的 8 位元無符號整數值。

(繼承來源 IDataRecord)
GetBytes(Int32, Int64, Byte[], Int32, Int32)

從指定的欄位偏移量讀取一串位元組到緩衝區,從指定的緩衝區偏移開始,作為陣列。

(繼承來源 IDataRecord)
GetChar(Int32)

取得指定欄位的字元值。

(繼承來源 IDataRecord)
GetChars(Int32, Int64, Char[], Int32, Int32)

從指定的欄位偏移量讀取一串字元到緩衝區,從指定的緩衝區偏移開始,作為陣列。

(繼承來源 IDataRecord)
GetData(Int32)

回傳指定欄位序數的 a IDataReader 。

(繼承來源 IDataRecord)
GetDataTypeName(Int32)

取得指定欄位的資料型別資訊。

(繼承來源 IDataRecord)
GetDateTime(Int32)

取得指定欄位的日期與時間資料值。

(繼承來源 IDataRecord)
GetDecimal(Int32)

取得指定欄位的固定位置數值。

(繼承來源 IDataRecord)
GetDouble(Int32)

取得指定欄位的雙精度浮點數。

(繼承來源 IDataRecord)
GetFieldType(Int32)

取得 Type 對應於 類型 Object 會從 GetValue(Int32)返回的資訊。

(繼承來源 IDataRecord)
GetFloat(Int32)

取得指定欄位的單精度浮點數。

(繼承來源 IDataRecord)
GetGuid(Int32)

回傳指定欄位的 GUID 值。

(繼承來源 IDataRecord)
GetInt16(Int32)

取得指定欄位的 16 位元有符號整數值。

(繼承來源 IDataRecord)
GetInt32(Int32)

取得指定欄位的 32 位元有號整數值。

(繼承來源 IDataRecord)
GetInt64(Int32)

取得指定欄位的 64 位元有符號整數值。

(繼承來源 IDataRecord)
GetName(Int32)

取得欄位要找到的名稱。

(繼承來源 IDataRecord)
GetOrdinal(String)

回傳命名欄位的索引。

(繼承來源 IDataRecord)
GetSchemaTable()

回傳描述 欄位元資料DataTable的 aIDataReader。

若執行指令未回傳結果集,則回傳 null ;若執行後 NextResult() 返回 false。

GetString(Int32)

取得指定欄位的字串值。

(繼承來源 IDataRecord)
GetValue(Int32)

回傳指定欄位的值。

(繼承來源 IDataRecord)
GetValues(Object[])

會將目前記錄的欄位值填充一組物件。

(繼承來源 IDataRecord)
IsDBNull(Int32)

回傳指定欄位是否設為 null。

(繼承來源 IDataRecord)
NextResult()

讀取批次 SQL 語句的結果時,將數據讀取器前進到下一個結果。

Read()

推進到 IDataReader 下一個紀錄。

適用於