IDataReader 介面
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供方法來讀取一或多個順向的結果集資料流 (這些結果集是在資料來源上執行命令而獲得的),而且是由存取關聯式資料庫的 .NET 資料提供者所實作。
public interface class IDataReader : IDisposable, System::Data::IDataRecord
public interface IDataReader : IDisposable, System.Data.IDataRecord
type IDataReader = interface
interface IDataRecord
interface IDisposable
type IDataReader = interface
interface IDisposable
interface IDataRecord
Public Interface IDataReader
Implements IDataRecord, IDisposable
- 衍生
- 實作
下列範例會建立衍生類別、 SqlConnection、 SqlCommand和 SqlDataReader的實例。 此範例會讀取數據,並將它寫出主控台。 最後,此範例會 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
IDataReader和 IDataRecord 介面允許繼承類別實DataReader
作 類別,以提供讀取一或多個結果集順向數據流的方法。 如需類別的詳細資訊 DataReader
,請參閱 使用 DataReader 擷取數據。
應用程式不會直接建立 介面的 IDataReader 實例,但會建立繼承 IDataReader之類別的實例。
繼承 IDataReader 的類別必須實作繼承的成員,而且通常會定義其他成員來新增提供者特定的功能。
當數據正在讀取時,對另一個進程或線程對結果集所做的變更,可能會由實 IDataReader
作 的類別使用者看到。 不過,精確的行為同時取決於提供者和計時。
若要在 .NET Framework 數據提供者之間提升一致性,請將繼承類別命名為 Command 格式Prv
,其中 Prv
是指定給特定 .NET Framework 數據提供者命名空間中所有類別的統一前置詞。 例如, Sql
是命名空間中 類別的SqlDataAdapterSystem.Data.SqlClient
前置詞。
使用者不會直接建立類別的 DataReader
實例。 相反地,它們會透過 ExecuteReader
物件的方法Command
取得 DataReader
實例。 因此,您應該將建構函式標示 DataReader
為內部。
Depth |
取得值,表示目前資料列的巢狀深度。 |
Field |
取得目前資料列中的資料行數目。 (繼承來源 IDataRecord) |
Is |
取得值,指出資料讀取器是否已關閉。 |
Item[Int32] |
取得位於指定索引位置的資料行。 (繼承來源 IDataRecord) |
Item[String] |
取得具有指定名稱的資料行。 (繼承來源 IDataRecord) |
Records |
取得因執行 SQL 陳述式所變更、插入或刪除的資料列數目。 |
Close() |
關閉 IDataReader 物件。 |
Dispose() |
執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之應用程式定義的工作。 (繼承來源 IDisposable) |
Get |
取得指定之資料行的值做為布林值 (Boolean)。 (繼承來源 IDataRecord) |
Get |
取得指定資料行的 8 位元不帶正負號的整數值。 (繼承來源 IDataRecord) |
Get |
從指定的緩衝區位移開始,將來自指定資料行位移的位元組資料流讀取到緩衝區以做為陣列。 (繼承來源 IDataRecord) |
Get |
取得指定資料行的字元值。 (繼承來源 IDataRecord) |
Get |
從指定的緩衝區位移開始,將來自指定資料行位移的字元資料流讀取到緩衝區以做為陣列。 (繼承來源 IDataRecord) |
Get |
傳回指定之資料行序數的 IDataReader。 (繼承來源 IDataRecord) |
Get |
取得指定欄位的資料類型資訊。 (繼承來源 IDataRecord) |
Get |
取得指定欄位的日期和時間資料值。 (繼承來源 IDataRecord) |
Get |
取得指定欄位的固定位置數值。 (繼承來源 IDataRecord) |
Get |
取得指定欄位的雙精確度浮點數。 (繼承來源 IDataRecord) |
Get |
取得對應至 Object 類型的 Type 資訊,前者是從 GetValue(Int32) 所傳回。 (繼承來源 IDataRecord) |
Get |
取得指定欄位的單精確度浮點數。 (繼承來源 IDataRecord) |
Get |
傳回指定欄位的 GUID 值。 (繼承來源 IDataRecord) |
Get |
取得指定欄位的 16 位元帶正負號的整數值。 (繼承來源 IDataRecord) |
Get |
取得指定欄位的 32 位元帶正負號的整數值。 (繼承來源 IDataRecord) |
Get |
取得指定欄位的 64 位元帶正負號的整數值。 (繼承來源 IDataRecord) |
Get |
取得要尋找的欄位名稱。 (繼承來源 IDataRecord) |
Get |
傳回具名欄位的索引。 (繼承來源 IDataRecord) |
Get |
傳回 DataTable,以描述 IDataReader 的資料行中繼資料。
|
Get |
取得指定欄位的字串值。 (繼承來源 IDataRecord) |
Get |
傳回指定欄位的值。 (繼承來源 IDataRecord) |
Get |
使用目前記錄的資料行值填入物件的陣列。 (繼承來源 IDataRecord) |
Is |
傳回指定的欄位是否設定為 Null。 (繼承來源 IDataRecord) |
Next |
在讀取批次 SQL 陳述式的結果時,會將資料讀取器向前移到下一個結果。 |
Read() |
將 IDataReader 推進到下一筆記錄。 |
產品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |