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
衍生
實作

範例

下列範例會建立衍生類別 、 SqlConnectionSqlCommandSqlDataReader 的實例。 此範例會讀取資料,並將它寫出主控台。 最後,此範例會 SqlDataReader 關閉 ,然後關閉 SqlConnection

using System;
using System.Data;
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        string str = "Data Source=(local);Initial Catalog=Northwind;"
            + "Integrated Security=SSPI";
        ReadOrderData(str);
    }

    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]));
    }
}
Option Explicit On
Option Strict On

Imports System.Data
Imports System.Data.SqlClient

Module Module1

    Sub Main()
        Dim str As String = "Data Source=(local);Initial Catalog=Northwind;" _
       & "Integrated Security=SSPI;"
        ReadOrderData(str)
    End Sub

    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

End Module

備註

IDataReaderIDataRecord 介面允許繼承類別實 DataReader 作 類別,以提供讀取一或多個結果集順向資料流程的方法。 如需類別的詳細資訊 DataReader ,請參閱 使用 DataReader 擷取資料

應用程式不會直接建立 介面的 IDataReader 實例,但會建立繼承 IDataReader 之類別的實例。

繼承 IDataReader 的類別必須實作繼承的成員,而且通常會定義其他成員來新增提供者特定的功能。

當資料正在讀取時,對另一個進程或執行緒對結果集所做的變更,可能會由實 IDataReader 作 的類別使用者看到。 不過,精確的行為同時取決於提供者和計時。

給實施者的注意事項

若要在.NET Framework資料提供者之間提升一致性,請將繼承類別命名為 Command 格式 Prv ,其中 Prv 是指定給特定.NET Framework資料提供者命名空間中所有類別的統一前置詞。 例如, Sql 是 命名空間中 類別的 SqlDataAdapterSystem.Data.SqlClient 前置詞。

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

屬性

Depth

取得值,表示目前資料列的巢狀深度。

FieldCount

取得目前資料列中的資料行數目。

(繼承來源 IDataRecord)
IsClosed

取得值,指出資料讀取器是否已關閉。

Item[Int32]

取得位於指定索引位置的資料行。

(繼承來源 IDataRecord)
Item[String]

取得具有指定名稱的資料行。

(繼承來源 IDataRecord)
RecordsAffected

取得因執行 SQL 陳述式所變更、插入或刪除的資料列數目。

方法

Close()

關閉 IDataReader 物件。

Dispose()

執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之應用程式定義的工作。

(繼承來源 IDisposable)
GetBoolean(Int32)

取得指定之資料行的值做為布林值 (Boolean)。

(繼承來源 IDataRecord)
GetByte(Int32)

取得指定資料行的 8 位元不帶正負號的整數值。

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

從指定的緩衝區位移開始,將來自指定資料行位移的位元組資料流讀取到緩衝區以做為陣列。

(繼承來源 IDataRecord)
GetChar(Int32)

取得指定資料行的字元值。

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

從指定的緩衝區位移開始,將來自指定資料行位移的字元資料流讀取到緩衝區以做為陣列。

(繼承來源 IDataRecord)
GetData(Int32)

傳回指定之資料行序數的 IDataReader

(繼承來源 IDataRecord)
GetDataTypeName(Int32)

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

(繼承來源 IDataRecord)
GetDateTime(Int32)

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

(繼承來源 IDataRecord)
GetDecimal(Int32)

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

(繼承來源 IDataRecord)
GetDouble(Int32)

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

(繼承來源 IDataRecord)
GetFieldType(Int32)

取得對應至 Object 類型的 Type 資訊,前者是從 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,以描述 IDataReader 的資料行中繼資料。

null如果執行的命令未傳回任何結果集,或傳回 之後 NextResult() ,則傳 false 回 。

GetString(Int32)

取得指定欄位的字串值。

(繼承來源 IDataRecord)
GetValue(Int32)

傳回指定欄位的值。

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

使用目前記錄的資料行值填入物件的陣列。

(繼承來源 IDataRecord)
IsDBNull(Int32)

傳回指定的欄位是否設定為 Null。

(繼承來源 IDataRecord)
NextResult()

在讀取批次 SQL 陳述式的結果時,會將資料讀取器向前移到下一個結果。

Read()

IDataReader 推進到下一筆記錄。

適用於