IDataReader IDataReader IDataReader IDataReader Interface

定義

提供一種方法來讀取一或多個順向的結果集資料流 (這些結果集是在資料來源上執行命令而獲得的),而且是由存取關聯式資料庫的 .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
衍生
實作

範例

下列範例會建立衍生的類別的執行個體SqlConnectionSqlCommand,和SqlDataReaderThe 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,然後在SqlConnectionFinally, 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

備註

IDataReaderIDataRecord介面允許繼承的類別,來實作DataReader類別,可提供方法來讀取一或多個順向資料流之結果集。The 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類別,請參閱擷取的資料使用 DataReaderFor more information about DataReader classes, see Retrieving Data Using a DataReader.

應用程式不會建立的執行個體IDataReader介面直接,但建立繼承的類別的執行個體IDataReaderAn 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.

處理結果集,另一個所做的變更,或執行緒正在讀取的資料可能會顯示給使用者,是實作的類別,而IDataReaderChanges 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 資料提供者命名空間中的所有類別。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. 例如,Sql是的前置詞SqlDataAdapter類別中System.Data.SqlClient命名空間。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執行個體透過ExecuteReader方法Command物件。Instead, they obtain the DataReader instance through the ExecuteReader method of the Command object. 因此,您應該將標示DataReader為內部的建構函式。Therefore, you should mark DataReader constructors as internal.

屬性

Depth Depth Depth Depth

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

FieldCount FieldCount FieldCount FieldCount

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

(Inherited from IDataRecord)
IsClosed IsClosed IsClosed IsClosed

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

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

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

(Inherited from IDataRecord)
Item[String] Item[String] Item[String] Item[String]

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

(Inherited from IDataRecord)
RecordsAffected RecordsAffected RecordsAffected RecordsAffected

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

方法

Close() Close() Close() Close()

關閉 IDataReader 物件。Closes the IDataReader Object.

Dispose() Dispose() Dispose() Dispose()

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

(Inherited from IDisposable)
GetBoolean(Int32) GetBoolean(Int32) GetBoolean(Int32) GetBoolean(Int32)

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

(Inherited from IDataRecord)
GetByte(Int32) GetByte(Int32) GetByte(Int32) GetByte(Int32)

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

(Inherited from IDataRecord)
GetBytes(Int32, Int64, Byte[], Int32, Int32) GetBytes(Int32, Int64, Byte[], Int32, Int32) GetBytes(Int32, Int64, Byte[], Int32, Int32) 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.

(Inherited from IDataRecord)
GetChar(Int32) GetChar(Int32) GetChar(Int32) GetChar(Int32)

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

(Inherited from IDataRecord)
GetChars(Int32, Int64, Char[], Int32, Int32) GetChars(Int32, Int64, Char[], Int32, Int32) GetChars(Int32, Int64, Char[], Int32, Int32) 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.

(Inherited from IDataRecord)
GetData(Int32) GetData(Int32) GetData(Int32) GetData(Int32)

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

(Inherited from IDataRecord)
GetDataTypeName(Int32) GetDataTypeName(Int32) GetDataTypeName(Int32) GetDataTypeName(Int32)

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

(Inherited from IDataRecord)
GetDateTime(Int32) GetDateTime(Int32) GetDateTime(Int32) GetDateTime(Int32)

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

(Inherited from IDataRecord)
GetDecimal(Int32) GetDecimal(Int32) GetDecimal(Int32) GetDecimal(Int32)

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

(Inherited from IDataRecord)
GetDouble(Int32) GetDouble(Int32) GetDouble(Int32) GetDouble(Int32)

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

(Inherited from IDataRecord)
GetFieldType(Int32) GetFieldType(Int32) GetFieldType(Int32) GetFieldType(Int32)

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

(Inherited from IDataRecord)
GetFloat(Int32) GetFloat(Int32) GetFloat(Int32) GetFloat(Int32)

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

(Inherited from IDataRecord)
GetGuid(Int32) GetGuid(Int32) GetGuid(Int32) GetGuid(Int32)

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

(Inherited from IDataRecord)
GetInt16(Int32) GetInt16(Int32) GetInt16(Int32) GetInt16(Int32)

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

(Inherited from IDataRecord)
GetInt32(Int32) GetInt32(Int32) GetInt32(Int32) GetInt32(Int32)

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

(Inherited from IDataRecord)
GetInt64(Int32) GetInt64(Int32) GetInt64(Int32) GetInt64(Int32)

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

(Inherited from IDataRecord)
GetName(Int32) GetName(Int32) GetName(Int32) GetName(Int32)

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

(Inherited from IDataRecord)
GetOrdinal(String) GetOrdinal(String) GetOrdinal(String) GetOrdinal(String)

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

(Inherited from IDataRecord)
GetSchemaTable() GetSchemaTable() GetSchemaTable() GetSchemaTable()

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

GetString(Int32) GetString(Int32) GetString(Int32) GetString(Int32)

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

(Inherited from IDataRecord)
GetValue(Int32) GetValue(Int32) GetValue(Int32) GetValue(Int32)

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

(Inherited from IDataRecord)
GetValues(Object[]) GetValues(Object[]) GetValues(Object[]) GetValues(Object[])

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

(Inherited from IDataRecord)
IsDBNull(Int32) IsDBNull(Int32) IsDBNull(Int32) IsDBNull(Int32)

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

(Inherited from IDataRecord)
NextResult() NextResult() NextResult() NextResult()

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

Read() Read() Read() Read()

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

適用於