SqlDataReader 類別

定義

提供從 SQL Server 資料庫中讀取順向資料流資料列的方式。 此類別無法獲得繼承。

public ref class SqlDataReader : System::Data::Common::DbDataReader, IDisposable
public ref class SqlDataReader : System::Data::Common::DbDataReader, IDisposable, System::Data::Common::IDbColumnSchemaGenerator
public ref class SqlDataReader : System::Data::Common::DbDataReader
public class SqlDataReader : System.Data.Common.DbDataReader, IDisposable
public class SqlDataReader : System.Data.Common.DbDataReader, IDisposable, System.Data.Common.IDbColumnSchemaGenerator
public class SqlDataReader : System.Data.Common.DbDataReader
type SqlDataReader = class
    inherit DbDataReader
    interface IDataReader
    interface IDisposable
    interface IDataRecord
type SqlDataReader = class
    inherit DbDataReader
    interface IDataReader
    interface IDataRecord
    interface IDisposable
type SqlDataReader = class
    inherit DbDataReader
    interface IDbColumnSchemaGenerator
    interface IDisposable
type SqlDataReader = class
    inherit DbDataReader
    interface IDbColumnSchemaGenerator
    interface IDataReader
    interface IDataRecord
    interface IDisposable
type SqlDataReader = class
    inherit DbDataReader
    interface IDisposable
Public Class SqlDataReader
Inherits DbDataReader
Implements IDisposable
Public Class SqlDataReader
Inherits DbDataReader
Implements IDbColumnSchemaGenerator, IDisposable
Public Class SqlDataReader
Inherits DbDataReader
繼承
SqlDataReader
實作

範例

下列範例會建立 SqlConnection 、、 SqlCommandSqlDataReader 。 此範例會讀取資料,並將它寫出主控台視窗。 然後程式碼會 SqlDataReader 關閉 。 會在 SqlConnection 程式碼區塊結尾 using 自動關閉 。

using Microsoft.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]));
    }

}

備註

若要建立 SqlDataReader ,您必須呼叫 ExecuteReader 物件的 方法 SqlCommand ,而不是直接使用建構函式。

SqlDataReader使用 時,相關聯的 SqlConnection 會忙於提供 SqlDataReader ,而且除了關閉作業之外,也無法對 SqlConnection 其他作業執行。 這是在呼叫 的 SqlDataReader 方法之前 Close 的情況。 例如,在呼叫 Close 之後,您才能擷取輸出參數。

正在讀取資料時,SqlDataReader 使用者會看到另一個處理序或執行緒對結果集所進行的變更。 但精確的行為取決於時間點。

IsClosedRecordsAffected 是在關閉 SqlDataReader 後唯一可呼叫的屬性。 RecordsAffected雖然 可以在 存在時 SqlDataReader 存取 屬性,但一律先呼叫 ,再傳 Close 回 的值 RecordsAffected ,以確保正確的傳回值。

使用循序存取 () CommandBehavior.SequentialAccess 時, InvalidOperationException 如果 SqlDataReader 位置是進階的,而且在上一個資料行上嘗試另一個讀取作業,則會引發 。

注意

為了獲得最佳效能, SqlDataReader 請避免建立不必要的物件或建立不必要的資料複本。 因此,對方法的多個呼叫,例如 GetValue 傳回相同物件的參考。 如果您要修改方法所傳回之物件的基礎值,請小心,例如 GetValue

屬性

Connection

取得與 SqlConnection 相關聯的 SqlDataReader

Depth

取得值,表示目前資料列的巢狀深度。

FieldCount

取得目前資料列中的資料行數目。

HasRows

取得值,指出 SqlDataReader 是否包含一個或多個資料列。

IsClosed

擷取布林值,指出指定的 SqlDataReader 執行個體是否已關閉。

Item[Int32]

提供資料行序數,取得使用原生格式的指定資料行值。

Item[String]

提供資料行名稱,取得使用原生格式的指定資料行值。

RecordsAffected

取得 Transact-SQL 陳述式的執行所變更、插入或刪除的資料列數目。

SensitivityClassification

SensitivityClassification使用 取得資訊 SqlDataReader

VisibleFieldCount

取得 SqlDataReader 中沒有隱藏的欄位數。

方法

Close()

關閉 SqlDataReader 物件。

Dispose(Boolean)

提供從 SQL Server 資料庫中讀取順向資料流資料列的方式。 此類別無法獲得繼承。

GetBoolean(Int32)

取得指定之資料行的值做為布林值 (Boolean)。

GetByte(Int32)

取得指定資料行的值做為位元組。

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

從指定資料行位移將位元組資料流讀取到緩衝區中,作為以指定的緩衝區位移開頭的陣列。

GetChar(Int32)

從指定的資料行取得單一字元形式的值。

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

從指定的資料行位移將字元資料流讀取到緩衝區中,作為以指定緩衝區位移開頭的陣列。

GetColumnSchema()

取得唯讀資料行結構描述集合。

GetDataTypeName(Int32)

取得字串,表示指定之資料行的資料型別。

GetDateTime(Int32)

取得指定之資料行的值做為 DateTime 物件。

GetDateTimeOffset(Int32)

擷取指定資料行的值做為 DateTimeOffset 物件。

GetDecimal(Int32)

取得指定之資料行的值做為 Decimal 物件。

GetDouble(Int32)

從指定的資料行取得雙精確度浮點數形式的值。

GetEnumerator()

傳回透過 IEnumerator 重複的 SqlDataReader

GetFieldType(Int32)

取得 Type,即是物件的資料型別。

GetFieldValue<T>(Int32)

同步取得指定之資料行的值做為型別。 GetFieldValueAsync<T>(Int32, CancellationToken) 是這個方法的非同步版本。

GetFieldValueAsync<T>(Int32, CancellationToken)

非同步取得指定之資料行的值做為型別。 GetFieldValue<T>(Int32) 是這個方法的同步版本。

GetFloat(Int32)

取得指定資料行的值做為單精確度浮點數。

GetGuid(Int32)

從指定的資料行取得通用唯一識別碼 (GUID) 形式的值。

GetInt16(Int32)

取得指定資料行的值做為 16 位元帶正負號的整數。

GetInt32(Int32)

取得指定資料行的值作為 32 位元帶正負號的整數。

GetInt64(Int32)

取得指定資料行的值做為 64 位元帶正負號的整數 (Signed Integer)。

GetName(Int32)

取得指定資料行的名稱。

GetOrdinal(String)

提供資料行的名稱,取得資料行序數。

GetProviderSpecificFieldType(Int32)

取得 Object,其為基礎提供者特定欄位型別的表示。

GetProviderSpecificValue(Int32)

取得 Object,其為基礎提供者特定值的表示。

GetProviderSpecificValues(Object[])

取得物件的陣列,這些物件是基礎提供者特定值的表示。

GetSchemaTable()

傳回 DataTable,以描述 SqlDataReader 的資料行中繼資料。

GetSqlBinary(Int32)

取得指定的資料行值做為 SqlBinary

GetSqlBoolean(Int32)

取得指定的資料行值做為 SqlBoolean

GetSqlByte(Int32)

取得指定的資料行值做為 SqlByte

GetSqlBytes(Int32)

取得指定資料行的值做為 SqlBytes

GetSqlChars(Int32)

取得指定資料行的值做為 SqlChars

GetSqlDateTime(Int32)

取得指定的資料行值做為 SqlDateTime

GetSqlDecimal(Int32)

取得指定的資料行值做為 SqlDecimal

GetSqlDouble(Int32)

取得指定的資料行值做為 SqlDouble

GetSqlGuid(Int32)

取得指定的資料行值做為 SqlGuid

GetSqlInt16(Int32)

取得指定的資料行值做為 SqlInt16

GetSqlInt32(Int32)

取得指定的資料行值做為 SqlInt32

GetSqlInt64(Int32)

取得指定的資料行值做為 SqlInt64

GetSqlMoney(Int32)

取得指定的資料行值做為 SqlMoney

GetSqlSingle(Int32)

取得指定的資料行值做為 SqlSingle

GetSqlString(Int32)

取得指定的資料行值做為 SqlString

GetSqlValue(Int32)

將指定之資料行內的資料值當做 SQL Server 型別傳回。

GetSqlValues(Object[])

填滿 Object 陣列,其中包含記錄內所有資料行 (表示為 SQL Server 型別) 的值。

GetSqlXml(Int32)

取得指定資料行的值做為 XML 值。

GetStream(Int32)

擷取二進位、影像、varbinary、UDT 和 variant 資料型別做為Stream

GetString(Int32)

取得指定的資料行值做為字串。

GetTextReader(Int32)

擷取 Char、NChar、 NText、 NVarChar、 文字、 varChar 及 Variant 資料型別做為TextReader

GetTimeSpan(Int32)

擷取指定資料行的值做為 TimeSpan 物件。

GetValue(Int32)

取得使用原生格式的指定資料行值。

GetValues(Object[])

使用目前資料列的資料行值填入物件陣列。

GetXmlReader(Int32)

擷取 XML 型別的資料做為XmlReader

IsCommandBehavior(CommandBehavior)

判斷指定的 CommandBehavior 是否與 SqlDataReader 的行為相符。

IsDBNull(Int32)

取得值,指出資料行是否含有不存在或遺漏的值。

IsDBNullAsync(Int32, CancellationToken)

IsDBNull(Int32) 的非同步版本,該版本會取得值,表示資料行是否包含不存在或遺漏的值。

取消語彙基元可用於要求在命令逾時之前捨棄作業。 例外狀況將經由傳回的 Task 物件回報。

NextResult()

當讀取批次 Transact-SQL 陳述式的結果時,將資料讀取器前移到下一個結果。

NextResultAsync(CancellationToken)

非同步版本的 NextResult() ,會在讀取批次 Transact-SQL 語句的結果時,將資料讀取器前進到下一個結果。

取消語彙基元可用於要求在命令逾時之前捨棄作業。 例外狀況將經由傳回的 Task 物件回報。

Read()

SqlDataReader 推進到下一筆記錄。

ReadAsync(CancellationToken)

非同步版本的 Read(),這個版本會將 SqlDataReader 前移至下一筆記錄。

取消語彙基元可用於要求在命令逾時之前捨棄作業。 例外狀況將經由傳回的 Task 物件回報。

明確介面實作

IDataRecord.GetData(Int32)

傳回指定之資料行序數的 IDataReader

適用於