IDataRecord 介面
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供 DataReader
每個資料列中資料行值的存取權,而且是由存取關聯式資料庫的 .NET 資料提供者所實作。
public interface class IDataRecord
public interface IDataRecord
type IDataRecord = interface
Public Interface IDataRecord
- 衍生
範例
下列範例會建立衍生類別、 SqlConnection、 SqlCommand和 的 SqlDataReader實例。 此範例會讀取數據,並將其寫出至主控台。 最後,此範例會 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
備註
IDataReader和 IDataRecord 介面可讓繼承類別實作 DataReader
類別。 這可讓您讀取結果集的一或多個順向數據流。 如需類別的詳細資訊 DataReader
,請參閱 使用 DataReader 擷取數據。
應用程式不會直接建立 介面的 IDataRecord 實例,但會建立繼承 IDataRecord之類別的實例。 一般而言,您可以透過 ExecuteReader
物件的方法Command
取得 DataReader
來執行此動作。
繼承 IDataRecord 的類別必須實作所有繼承的成員,而且通常會定義其他成員以新增提供者特定的功能。
實作 的 DataReader
提供者必須在 Common Language Runtime (CLR) 類型中公開數據。 類型強制型別是針對 CLR 中未包含的某些類型所定義。 這些值可以存取為符合 CLR 類型的替代類型。 例如,下表列出 OLE DB 數據類型與 CLR 類型的建議對應,其中括弧中有替代類型。
OLE DB 型別 | CLR 類型 |
---|---|
DBTYPE_BOOL | Int16 |
DBTYPE_BSTR | 字串 |
DBTYPE_BYTES | byte[] |
DBTYPE_CY | Decimal |
DBTYPE_DATE | DateTime |
DBTYPE_DBDATE | DateTime |
DBTYPE_DBTIME | DateTime |
DBTYPE_DBTIMESTAMP | DateTime |
DBTYPE_DECIMAL | Decimal |
DBTYPE_EMPTY | null |
DBTYPE_ERROR | ExternalException |
DBTYPE_FILETIME | DateTime |
DBTYPE_GUID | Guid |
DBTYPE_HCHAPTER | 不支援 |
DBTYPE_I1 | SByte |
DBTYPE_I2 | Int16 |
DBTYPE_I4 | Int32 |
DBTYPE_I8 | Int64 |
DBTYPE_IDISPATCH | 物件 (object) |
DBTYPE_IUNKNOWN | 物件 (object) |
DBTYPE_NULL | DBNull.Value |
DBTYPE_NUMERIC | Decimal |
DBTYPE_PROPVARIANT | 物件 (object) |
DBTYPE_R4 | Single |
DBTYPE_R8 | Double |
DBTYPE_STR | 字串 |
DBTYPE_UDT | 不支援 |
DBTYPE_UI1 | byte (Int16) |
DBTYPE_UI2 | UInt16 (Int32) |
DBTYPE_UI4 | UInt32 (Int64) |
DBTYPE_UI8 | UInt64 (Decimal) |
DBTYPE_VARIANT | 物件 (object) |
DBTYPE_VARNUMERIC | 不支援 |
DBTYPE_WSTR | 字串 |
屬性
FieldCount |
取得目前資料列中的資料行數目。 |
Item[Int32] |
取得位於指定索引位置的資料行。 |
Item[String] |
取得具有指定名稱的資料行。 |
方法
GetBoolean(Int32) |
取得指定之資料行的值做為布林值 (Boolean)。 |
GetByte(Int32) |
取得指定資料行的 8 位元不帶正負號的整數值。 |
GetBytes(Int32, Int64, Byte[], Int32, Int32) |
從指定的緩衝區位移開始,將來自指定資料行位移的位元組資料流讀取到緩衝區以做為陣列。 |
GetChar(Int32) |
取得指定資料行的字元值。 |
GetChars(Int32, Int64, Char[], Int32, Int32) |
從指定的緩衝區位移開始,將來自指定資料行位移的字元資料流讀取到緩衝區以做為陣列。 |
GetData(Int32) |
傳回指定之資料行序數的 IDataReader。 |
GetDataTypeName(Int32) |
取得指定欄位的資料類型資訊。 |
GetDateTime(Int32) |
取得指定欄位的日期和時間資料值。 |
GetDecimal(Int32) |
取得指定欄位的固定位置數值。 |
GetDouble(Int32) |
取得指定欄位的雙精確度浮點數。 |
GetFieldType(Int32) |
取得對應至 Object 類型的 Type 資訊,前者是從 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。 |
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應