IDataReader 介面

定義

提供一種方法來讀取一或多個順向的結果集資料流 (這些結果集是在資料來源上執行命令而獲得的),而且是由存取關聯式資料庫的 .NET Framework 資料提供者所實作。Provides a means of reading one or more forward-only streams of result sets obtained by executing a command at a data source, and is implemented by .NET Framework data providers that access relational databases.

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

範例

下列範例會建立衍生類別、 SqlConnection SqlCommand、和SqlDataReader的實例。The following example creates instances of the derived classes, SqlConnection, SqlCommand, and SqlDataReader. 此範例會讀取資料, 並將其寫出至主控台。The example reads through the data, writing it out to the console. 最後, 此範例會關閉SqlDataReader, SqlConnection然後關閉。Finally, the example closes the SqlDataReader, then the 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 record)
    {
        Console.WriteLine(String.Format("{0}, {1}", record[0], record[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

備註

和介面允許繼承類別執行類別, 以提供一種方法來讀取結果集的一或多個順向資料流程。 DataReader IDataReader IDataRecordThe IDataReader and IDataRecord interfaces allow an inheriting class to implement a DataReader class, which provides a means of reading one or more forward-only streams of result sets. 如需DataReader類別的詳細資訊, 請參閱使用 DataReader 抓取資料For more information about DataReader classes, see Retrieving Data Using a DataReader.

應用程式不會直接建立IDataReader介面的實例, 而是會建立繼承IDataReader之類別的實例。An application does not create an instance of the IDataReader interface directly, but creates an instance of a class that inherits IDataReader.

繼承IDataReader的類別必須執行繼承的成員, 而且通常會定義其他成員來新增提供者特有的功能。Classes that inherit IDataReader must implement the inherited members, and typically define additional members to add provider-specific functionality.

在讀取資料時, 由另一個進程或執行緒對結果集所做的變更, 可能會對執行的類別IDataReader的使用者顯示。Changes made to a result set by another process or thread while data is being read may be visible to the user of a class that implements an IDataReader. 不過, 精確的行為是提供者和時間相依。However, the precise behavior is both provider and timing dependent.

給實施者的注意事項

若要提升 .NET Framework 資料提供者之間的一致性, 請在 [窗Prv體] Prv命令中命名繼承類別, 其中是指定給特定 .NET Framework Data Provider 命名空間中所有類別的統一前置詞。To promote consistency among .NET Framework data providers, name the inheriting class in the form Prv Command where Prv is the uniform prefix given to all classes in a specific .NET Framework data provider namespace. 例如, SqlSystem.Data.SqlClient命名空間中SqlDataAdapter類別的前置詞。For example, Sql is the prefix of the SqlDataAdapter class in the System.Data.SqlClient namespace.

使用者不會直接建立DataReader類別的實例。Users do not create an instance of a DataReader class directly. 相反地, 它們會DataReader透過Command物件的ExecuteReader方法取得實例。Instead, they obtain the DataReader instance through the ExecuteReader method of the Command object. 因此, 您應該將DataReader 「函式」標示為「內部」。Therefore, you should mark DataReader constructors as internal.

屬性

Depth

取得值,表示目前資料列的巢狀深度。Gets a value indicating the depth of nesting for the current row.

FieldCount

取得目前資料列中的資料行數目。Gets the number of columns in the current row.

(繼承來源 IDataRecord)
IsClosed

取得值,指出資料讀取器是否已關閉。Gets a value indicating whether the data reader is closed.

Item[Int32]

取得位於指定索引位置的資料行。Gets the column located at the specified index.

(繼承來源 IDataRecord)
Item[String]

取得具有指定名稱的資料行。Gets the column with the specified name.

(繼承來源 IDataRecord)
RecordsAffected

取得因執行 SQL 陳述式所變更、插入或刪除的資料列數目。Gets the number of rows changed, inserted, or deleted by execution of the SQL statement.

方法

Close()

關閉 IDataReader 物件。Closes the IDataReader Object.

Dispose()

執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之應用程式定義的工作。Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

(繼承來源 IDisposable)
GetBoolean(Int32)

取得指定之資料行的值做為布林值 (Boolean)。Gets the value of the specified column as a Boolean.

(繼承來源 IDataRecord)
GetByte(Int32)

取得指定資料行的 8 位元不帶正負號的整數值。Gets the 8-bit unsigned integer value of the specified column.

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

從指定的緩衝區位移開始,將來自指定資料行位移的位元組資料流讀取到緩衝區以做為陣列。Reads a stream of bytes from the specified column offset into the buffer as an array, starting at the given buffer offset.

(繼承來源 IDataRecord)
GetChar(Int32)

取得指定資料行的字元值。Gets the character value of the specified column.

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

從指定的緩衝區位移開始,將來自指定資料行位移的字元資料流讀取到緩衝區以做為陣列。Reads a stream of characters from the specified column offset into the buffer as an array, starting at the given buffer offset.

(繼承來源 IDataRecord)
GetData(Int32)

傳回指定之資料行序數的 IDataReaderReturns an IDataReader for the specified column ordinal.

(繼承來源 IDataRecord)
GetDataTypeName(Int32)

取得指定欄位的資料類型資訊。Gets the data type information for the specified field.

(繼承來源 IDataRecord)
GetDateTime(Int32)

取得指定欄位的日期和時間資料值。Gets the date and time data value of the specified field.

(繼承來源 IDataRecord)
GetDecimal(Int32)

取得指定欄位的固定位置數值。Gets the fixed-position numeric value of the specified field.

(繼承來源 IDataRecord)
GetDouble(Int32)

取得指定欄位的雙精確度浮點數。Gets the double-precision floating point number of the specified field.

(繼承來源 IDataRecord)
GetFieldType(Int32)

取得對應至 Object 類型的 Type 資訊,前者是從 GetValue(Int32) 所傳回。Gets the Type information corresponding to the type of Object that would be returned from GetValue(Int32).

(繼承來源 IDataRecord)
GetFloat(Int32)

取得指定欄位的單精確度浮點數。Gets the single-precision floating point number of the specified field.

(繼承來源 IDataRecord)
GetGuid(Int32)

傳回指定欄位的 GUID 值。Returns the GUID value of the specified field.

(繼承來源 IDataRecord)
GetInt16(Int32)

取得指定欄位的 16 位元帶正負號的整數值。Gets the 16-bit signed integer value of the specified field.

(繼承來源 IDataRecord)
GetInt32(Int32)

取得指定欄位的 32 位元帶正負號的整數值。Gets the 32-bit signed integer value of the specified field.

(繼承來源 IDataRecord)
GetInt64(Int32)

取得指定欄位的 64 位元帶正負號的整數值。Gets the 64-bit signed integer value of the specified field.

(繼承來源 IDataRecord)
GetName(Int32)

取得要尋找的欄位名稱。Gets the name for the field to find.

(繼承來源 IDataRecord)
GetOrdinal(String)

傳回具名欄位的索引。Return the index of the named field.

(繼承來源 IDataRecord)
GetSchemaTable()

傳回 DataTable,以描述 IDataReader 的資料行中繼資料。Returns a DataTable that describes the column metadata of the IDataReader.

GetString(Int32)

取得指定欄位的字串值。Gets the string value of the specified field.

(繼承來源 IDataRecord)
GetValue(Int32)

傳回指定欄位的值。Return the value of the specified field.

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

使用目前記錄的資料行值填入物件的陣列。Populates an array of objects with the column values of the current record.

(繼承來源 IDataRecord)
IsDBNull(Int32)

傳回指定的欄位是否設定為 Null。Return whether the specified field is set to null.

(繼承來源 IDataRecord)
NextResult()

在讀取批次 SQL 陳述式的結果時,會將資料讀取器向前移到下一個結果。Advances the data reader to the next result, when reading the results of batch SQL statements.

Read()

IDataReader 推進到下一筆記錄。Advances the IDataReader to the next record.

適用於