通过


SqlDataReader 类

定义

提供从 SQL Server 数据库读取仅向前行流的方法。 无法继承此类。

public ref class SqlDataReader : System::Data::Common::DbDataReader, IDisposable
public ref class SqlDataReader sealed : MarshalByRefObject, IDisposable, System::Collections::IEnumerable, System::Data::IDataReader
public class SqlDataReader : System.Data.Common.DbDataReader, IDisposable
public sealed class SqlDataReader : MarshalByRefObject, IDisposable, System.Collections.IEnumerable, System.Data.IDataReader
type SqlDataReader = class
    inherit DbDataReader
    interface IDisposable
type SqlDataReader = class
    inherit MarshalByRefObject
    interface IEnumerable
    interface IDataReader
    interface IDisposable
    interface IDataRecord
type SqlDataReader = class
    inherit DbDataReader
    interface IDataReader
    interface IDisposable
    interface IDataRecord
Public Class SqlDataReader
Inherits DbDataReader
Implements IDisposable
Public NotInheritable Class SqlDataReader
Inherits MarshalByRefObject
Implements IDataReader, IDisposable, IEnumerable
继承
SqlDataReader
继承
继承
SqlDataReader
实现

示例

以下示例创建一个 SqlConnection、一个 SqlCommand和一个 SqlDataReader。 该示例读取数据,并将其写出到控制台窗口。 然后,代码关闭 .SqlDataReader 代码 SqlConnection 块末尾 using 自动关闭。

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

注解

若要创建, SqlDataReader必须调用 ExecuteReader 对象的方法 SqlCommand ,而不是直接使用构造函数。

在使用时 SqlDataReader ,关联的 SqlConnection 服务正忙于提供服务 SqlDataReader,除了关闭其他操作之外,其他任何操作都无法执行 SqlConnection 。 这是在调用方法SqlDataReader之前Close的情况。 例如,在调用 Close后才能检索输出参数。

正在读取数据时对另一个进程或线程对结果集所做的更改可能对用户 SqlDataReader可见。 但是,精确行为取决于时间。

IsClosedRecordsAffected 关闭后 SqlDataReader 可以调用的唯一属性。 RecordsAffected虽然在存在时SqlDataReader可以访问该属性,但始终在返回值RecordsAffected之前调用Close以确保准确的返回值。

使用顺序访问时(CommandBehavior.SequentialAccess如果 InvalidOperationException 位置是高级的,并且尝试对上一列执行另一个读取操作,则会引发 SqlDataReader 一个读取操作。

注释

为获得最佳性能, SqlDataReader 请避免创建不必要的对象或创建不必要的数据副本。 因此,对方法的多次调用,例如 GetValue 返回对同一对象的引用。 如果要修改方法返回的对象的基础值,请使用 GetValue谨慎。

属性

名称 说明
Connection

获取与 SqlConnection . SqlDataReader.

Depth

获取一个值,该值指示当前行的嵌套深度。

FieldCount

获取当前行中的列数。

HasRows

获取一个值,该值指示是否 SqlDataReader 包含一行或多行。

IsClosed

检索一个布尔值,该值指示指定的 SqlDataReader 实例是否已关闭。

Item[Int32]

获取给定列序号的指定列的本机格式的值。

Item[String]

获取给定列名称的本机格式指定列的值。

RecordsAffected

获取通过执行 Transact-SQL 语句更改、插入或删除的行数。

VisibleFieldCount

获取不隐藏的 SqlDataReader 字段数。

方法

名称 说明
Close()

关闭 SqlDataReader 对象。

CreateObjRef(Type)

创建一个对象,其中包含生成用于与远程对象通信的代理所需的所有相关信息。

(继承自 MarshalByRefObject)
Dispose()

释放类的 DbDataReader 当前实例使用的所有资源。

(继承自 DbDataReader)
Dispose(Boolean)

释放由托管资源使用 DbDataReader 的非托管资源,并选择性地释放托管资源。

(继承自 DbDataReader)
Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetBoolean(Int32)

获取指定列的值作为布尔值。

GetByte(Int32)

获取指定列作为字节的值。

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

将指定列偏移量的字节流读取到从给定缓冲区偏移量开始的数组的缓冲区中。

GetChar(Int32)

获取指定列的值作为单个字符。

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

将指定列偏移量中的字符流作为从给定缓冲区偏移量开始的数组读取到缓冲区中。

GetData(Int32)

返回指定列序号的一个 IDataReader

GetData(Int32)

返回所请求列的嵌套数据读取器。

(继承自 DbDataReader)
GetDataTypeName(Int32)

获取表示指定列的数据类型的字符串。

GetDateTime(Int32)

获取作为对象的指定列 DateTime 的值。

GetDateTimeOffset(Int32)

检索指定列的值作为 DateTimeOffset 对象。

GetDbDataReader(Int32)

返回 DbDataReader 请求的列序号的对象,该对象可通过提供程序特定的实现重写。

(继承自 DbDataReader)
GetDecimal(Int32)

获取作为对象的指定列 Decimal 的值。

GetDouble(Int32)

获取指定列的值作为双精度浮点数。

GetEnumerator()

返回循环访问的SqlDataReader一个 IEnumerator

GetFieldType(Int32)

获取 Type 对象数据类型。

GetFieldValue<T>(Int32)

同步获取指定列的值作为类型。 GetFieldValueAsync<T>(Int32, CancellationToken) 是此方法的异步版本。

GetFieldValueAsync<T>(Int32, CancellationToken)

以异步方式获取指定列的值作为类型。 GetFieldValue<T>(Int32) 是此方法的同步版本。

GetFieldValueAsync<T>(Int32)

以异步方式获取指定列的值作为请求的类型。

(继承自 DbDataReader)
GetFloat(Int32)

获取指定列的值作为单精度浮点数。

GetGuid(Int32)

获取指定列的值作为全局唯一标识符(GUID)。

GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetInt16(Int32)

获取指定列作为 16 位有符号整数的值。

GetInt32(Int32)

获取指定列作为 32 位有符号整数的值。

GetInt64(Int32)

获取指定列的值作为 64 位有符号整数。

GetLifetimeService()
已过时.

检索控制此实例的生存期策略的当前生存期服务对象。

(继承自 MarshalByRefObject)
GetName(Int32)

获取指定列的名称。

GetOrdinal(String)

获取列序号,给定列的名称。

GetProviderSpecificFieldType(Int32)

Object获取基础提供程序特定的字段类型的表示形式。

GetProviderSpecificValue(Int32)

获取一个 Object 是基础提供程序特定值的表示形式。

GetProviderSpecificValues(Object[])

获取作为基础提供程序特定值的表示形式的对象的数组。

GetSchemaTable()

返回一个描述列元数据的 SqlDataReader.DataTable

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[])

填充包含记录中所有列的值(以 SQL Server 类型表示)的数组 Object

GetSqlXml(Int32)

获取指定列的值作为 XML 值。

GetStream(Int32)

将二进制、图像、varbinary、UDT 和变体数据类型作为一种 Stream检索 。

GetString(Int32)

获取指定列作为字符串的值。

GetTextReader(Int32)

检索 Char、NChar、NText、NVarChar、text、varChar 和 Variant 数据类型。TextReader

GetTimeSpan(Int32)

检索指定列的值作为 TimeSpan 对象。

GetType()

获取当前实例的 Type

(继承自 Object)
GetValue(Int32)

获取指定列的本机格式的值。

GetValues(Object[])

使用当前行的列值填充对象的数组。

GetXmlReader(Int32)

检索 XML 类型的数据作为 .XmlReader

InitializeLifetimeService()
已过时.

获取生存期服务对象来控制此实例的生存期策略。

(继承自 MarshalByRefObject)
IsCommandBehavior(CommandBehavior)

确定指定的 CommandBehavior 匹配项是否与 . SqlDataReader .

IsDBNull(Int32)

获取一个值,该值指示列是包含不存在值还是缺失值。

IsDBNullAsync(Int32, CancellationToken)

异步版本的 IsDBNull(Int32) 获取一个值,该值指示列是包含不存在值还是缺失值。

取消令牌可用于请求在命令超时之前放弃操作。 异常将通过返回的任务对象报告。

IsDBNullAsync(Int32)

异步获取一个值,该值指示列是包含不存在值还是缺失值。

(继承自 DbDataReader)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。

(继承自 MarshalByRefObject)
NextResult()

读取批处理 Transact-SQL 语句的结果时,将数据读取器推进到下一个结果。

NextResultAsync()

读取一批语句的结果时,异步将读取器提升到下一个结果。

(继承自 DbDataReader)
NextResultAsync(CancellationToken)

读取批处理 Transact-SQL 语句的结果时,异步版本的 NextResult()数据读取器将数据读取器提升到下一个结果。

取消令牌可用于请求在命令超时之前放弃操作。 异常将通过返回的任务对象报告。

Read()

前进 SqlDataReader 到下一条记录。

ReadAsync()

异步将读取器前进到结果集中的下一条记录。

(继承自 DbDataReader)
ReadAsync(CancellationToken)

异步版本的 Read(),它将提升 SqlDataReader 到下一条记录。

取消令牌可用于请求在命令超时之前放弃操作。 异常将通过返回的任务对象报告。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

显式接口实现

名称 说明
IDataReader.Close()

有关此成员的说明,请参阅 Close()

(继承自 DbDataReader)
IDataReader.GetSchemaTable()

有关此成员的说明,请参阅 GetSchemaTable()

(继承自 DbDataReader)
IDataRecord.GetData(Int32)

返回指定列序号的一个 IDataReader

IDataRecord.GetData(Int32)

有关此成员的说明,请参阅 GetData(Int32)

(继承自 DbDataReader)
IDisposable.Dispose()

释放数据读取器使用的所有资源。

IEnumerable.GetEnumerator()

返回可用于循环访问项集合的枚举器。

扩展方法

名称 说明
AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

CanGetColumnSchema(DbDataReader)

获取一个值,该值指示是否可以 DbDataReader 获取列架构。

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定类型。

GetColumnSchema(DbDataReader)

获取 .DbDataReader. 的列架构 (DbColumn集合)

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。

适用于

另请参阅