IDataRecord 介面

定義

針對 DataReader 提供存取每個資料列中資料行值的權限,而且是由存取關聯式資料庫的 .NET Framework 資料提供者所實作。Provides access to the column values within each row for a DataReader, and is implemented by .NET Framework data providers that access relational databases.

public interface class IDataRecord
public interface IDataRecord
type IDataRecord = interface
Public Interface IDataRecord
衍生

範例

下列範例會建立衍生類別、 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, and 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 enable an inheriting class to implement a DataReader class. 這會提供一種方式來讀取結果集的一或多個順向資料流程。This provides a way 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.

應用程式不會直接建立IDataRecord介面的實例, 而是會建立繼承IDataRecord之類別的實例。An application does not create an instance of the IDataRecord interface directly, but creates an instance of a class that inherits IDataRecord. 一般來說, 您可以透過DataReader Command物件的ExecuteReader方法取得來完成這項工作。Typically, you do this by obtaining a DataReader through the ExecuteReader method of the Command object.

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

DataReader必須有提供者, 才能在 common language runtime (CLR) 類型中公開資料。Providers implementing a DataReader are required to expose data in common language runtime (CLR) types. 類型強制型轉是針對 CLR 中未包含的部分類型所定義。Type coercion is defined for some types not included in the CLR. 這些值可能會當做符合 CLR 類型的替代類型來存取。These values may be accessed as alternative types that comply with CLR types. 例如, 下表列出從 OLE DB 資料類型到 CLR 類型的建議對應, 並以括弧括住替代類型。As an example, the following table lists suggested mappings from OLE DB data types to CLR types, with alternative types in parentheses.

OLE DB 型別OLE DB type CLR 類型CLR type
DBTYPE_BOOLDBTYPE_BOOL Int16Int16
DBTYPE_BSTRDBTYPE_BSTR 字串string
DBTYPE_BYTESDBTYPE_BYTES byte[]byte[]
DBTYPE_CYDBTYPE_CY DecimalDecimal
DBTYPE_DATEDBTYPE_DATE DateTimeDateTime
DBTYPE_DBDATEDBTYPE_DBDATE DateTimeDateTime
DBTYPE_DBTIMEDBTYPE_DBTIME DateTimeDateTime
DBTYPE_DBTIMESTAMPDBTYPE_DBTIMESTAMP DateTimeDateTime
DBTYPE_DECIMALDBTYPE_DECIMAL DecimalDecimal
DBTYPE_EMPTYDBTYPE_EMPTY nullnull
DBTYPE_ERRORDBTYPE_ERROR ExternalExceptionExternalException
DBTYPE_FILETIMEDBTYPE_FILETIME DateTimeDateTime
DBTYPE_GUIDDBTYPE_GUID GuidGuid
DBTYPE_HCHAPTERDBTYPE_HCHAPTER 不支援not supported
DBTYPE_I1DBTYPE_I1 SByteSByte
DBTYPE_I2DBTYPE_I2 Int16Int16
DBTYPE_I4DBTYPE_I4 Int32Int32
DBTYPE_I8DBTYPE_I8 Int64Int64
DBTYPE_IDISPATCHDBTYPE_IDISPATCH 物件 (object)object
DBTYPE_IUNKNOWNDBTYPE_IUNKNOWN 物件 (object)object
DBTYPE_NULLDBTYPE_NULL DBNull.ValueDBNull.Value
DBTYPE_NUMERICDBTYPE_NUMERIC DecimalDecimal
DBTYPE_PROPVARIANTDBTYPE_PROPVARIANT 物件 (object)object
DBTYPE_R4DBTYPE_R4 SingleSingle
DBTYPE_R8DBTYPE_R8 DoubleDouble
DBTYPE_STRDBTYPE_STR 字串string
DBTYPE_UDTDBTYPE_UDT 不支援not supported
DBTYPE_UI1DBTYPE_UI1 byte (Int16)byte (Int16)
DBTYPE_UI2DBTYPE_UI2 UInt16 (Int32)UInt16 (Int32)
DBTYPE_UI4DBTYPE_UI4 UInt32 (Int64)UInt32 (Int64)
DBTYPE_UI8DBTYPE_UI8 UInt64 (十進位)UInt64 (Decimal)
DBTYPE_VARIANTDBTYPE_VARIANT 物件 (object)object
DBTYPE_VARNUMERICDBTYPE_VARNUMERIC 不支援not supported
DBTYPE_WSTRDBTYPE_WSTR 字串string

屬性

FieldCount

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

Item[Int32]

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

Item[String]

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

方法

GetBoolean(Int32)

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

GetByte(Int32)

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

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.

GetChar(Int32)

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

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.

GetData(Int32)

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

GetDataTypeName(Int32)

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

GetDateTime(Int32)

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

GetDecimal(Int32)

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

GetDouble(Int32)

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

GetFieldType(Int32)

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

GetFloat(Int32)

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

GetGuid(Int32)

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

GetInt16(Int32)

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

GetInt32(Int32)

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

GetInt64(Int32)

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

GetName(Int32)

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

GetOrdinal(String)

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

GetString(Int32)

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

GetValue(Int32)

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

GetValues(Object[])

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

IsDBNull(Int32)

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

適用於