IDataRecord 介面

定義

提供存取 ,針對 ,每 DataReader列內的欄位值,並由存取關聯式資料庫的 .NET 資料提供者實作。

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

範例

以下範例建立了 SqlConnection導出類別 、 、 SqlCommandSqlDataReader的實例。 此範例會讀取數據,並將其寫出至主控台。 最後,範例封 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

備註

IDataReaderIDataRecord介面使繼承類別能夠實作類別。DataReader 這提供了一種讀取一個或多個僅向前的結果集串流的方法。 欲了解更多類別 DataReader 資訊,請參閱 使用 DataReader 檢索資料

應用程式不會直接建立介面的實例 IDataRecord ,而是建立一個繼承 的 IDataRecord類別實例。 通常,你透過物件的方法Command取得 。DataReaderExecuteReader

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

實作 的 DataReader 提供者必須以通用語言執行時(CLR)類型公開資料。 型別強制力對於某些未包含於 CLR 中的型別定義。 這些值可作為符合 CLR 類型的替代類型存取。 以下表格列出了從 OLE DB 資料型態到 CLR 類型的建議映射,括號內列出了其他類型。

OLE DB 類型 CLR 類型
DBTYPE_BOOL Int16
DBTYPE_BSTR 字串
DBTYPE_BYTES byte[]
DBTYPE_CY 十進位
DBTYPE_DATE 約會時間
DBTYPE_DBDATE 約會時間
DBTYPE_DBTIME 約會時間
DBTYPE_DBTIMESTAMP 約會時間
DBTYPE_DECIMAL(十進位資料型別) 十進位
DBTYPE_EMPTY null
類型錯誤(DBTYPE_ERROR) 外部例外
DBTYPE_FILETIME 約會時間
DBTYPE_GUID (全域唯一識別碼) GUID
DBTYPE_HCHAPTER 不支援
DBTYPE_I1 SByte
DBTYPE_I2 Int16
DBTYPE_I4 Int32
DBTYPE_I8 Int64
DBTYPE_IDISPATCH 物件
DBTYPE_IUNKNOWN 物件
DBTYPE_NULL DBNull.價值
DBTYPE_NUMERIC 十進位
DBTYPE_PROPVARIANT (屬性變體類型) 物件
DBTYPE_R4 Single
DBTYPE_R8 Double
DBTYPE_STR 字串
DBTYPE_UDT 不支援
DBTYPE_UI1 位元組(Int16)
DBTYPE_UI2 UInt16 (Int32)
DBTYPE_UI4 UInt32(Int64)
DBTYPE_UI8 UInt64(小數位)
DBTYPE_VARIANT 物件
DBTYPE_VARNUMERIC 不支援
DBTYPE_WSTR 字串

屬性

名稱 Description
FieldCount

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

Item[Int32]

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

Item[String]

取得指定名稱的欄位。

方法

名稱 Description
GetBoolean(Int32)

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

GetByte(Int32)

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

GetBytes(Int32, Int64, Byte[], Int32, Int32)

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

GetChar(Int32)

取得指定欄位的字元值。

GetChars(Int32, Int64, Char[], Int32, Int32)

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

GetData(Int32)

回傳指定欄位序數的 a IDataReader

GetDataTypeName(Int32)

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

GetDateTime(Int32)

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

GetDecimal(Int32)

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

GetDouble(Int32)

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

GetFieldType(Int32)

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

GetFloat(Int32)

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

GetGuid(Int32)

回傳指定欄位的 GUID 值。

GetInt16(Int32)

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

GetInt32(Int32)

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

GetInt64(Int32)

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

GetName(Int32)

取得欄位要找到的名稱。

GetOrdinal(String)

回傳命名欄位的索引。

GetString(Int32)

取得指定欄位的字串值。

GetValue(Int32)

回傳指定欄位的值。

GetValues(Object[])

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

IsDBNull(Int32)

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

適用於