IDataReader IDataReader IDataReader IDataReader Interface

定义

提供一种读取结果集(通过对数据源执行命令获取)的一个或多个只进流的方法,并由访问关系数据库的.NET Framework 数据提供程序实现。Provides a means of reading one or more forward-only streams of result sets obtained by executing a command at a data source, and is implemented by .NET Framework data providers that access relational databases.

public interface class IDataReader : IDisposable, System::Data::IDataRecord
public interface IDataReader : IDisposable, System.Data.IDataRecord
type IDataReader = interface
    interface IDisposable
    interface IDataRecord
Public Interface IDataReader
Implements IDataRecord, IDisposable
派生
实现

示例

下面的示例创建派生类、 SqlConnection SqlCommand、和SqlDataReader的实例。The following example creates instances of the derived classes, SqlConnection, SqlCommand, and SqlDataReader. 该示例将读取数据, 并将其写出到控制台。The example reads through the data, writing it out to the console. 最后, 该示例关闭SqlDataReader, SqlConnection然后关闭。Finally, the example closes the SqlDataReader, then the 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 record)
    {
        Console.WriteLine(String.Format("{0}, {1}", record[0], record[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

注解

和接口允许继承类实现类, 该类提供一种读取结果集的一个或多个只进流的方法。 DataReader IDataReader IDataRecordThe IDataReader and IDataRecord interfaces allow an inheriting class to implement a DataReader class, which provides a means of reading one or more forward-only streams of result sets. 有关DataReader类的详细信息, 请参阅使用 DataReader 检索数据For more information about DataReader classes, see Retrieving Data Using a DataReader.

应用程序不会直接创建IDataReader接口的实例, 而是创建继承IDataReader的类的实例。An application does not create an instance of the IDataReader interface directly, but creates an instance of a class that inherits IDataReader.

继承IDataReader的类必须实现继承成员, 并且通常定义其他成员来添加特定于提供程序的功能。Classes that inherit IDataReader must implement the inherited members, and typically define additional members to add provider-specific functionality.

在读取数据时, 由其他进程或线程对结果集所做的更改可能对实现的IDataReader类的用户可见。Changes made to a result set by another process or thread while data is being read may be visible to the user of a class that implements an IDataReader. 但确切的行为是提供程序和计时依赖项。However, the precise behavior is both provider and timing dependent.

实施者说明

若要提升 .NET Framework 数据提供程序之间的一致性, 请在 form Prv命令中为继承类命名, 其中Prv是为特定 .NET Framework 数据提供程序命名空间中的所有类指定的统一前缀。To promote consistency among .NET Framework data providers, name the inheriting class in the form Prv Command where Prv is the uniform prefix given to all classes in a specific .NET Framework data provider namespace. 例如, SqlSystem.Data.SqlClient命名空间中SqlDataAdapter类的前缀。For example, Sql is the prefix of the SqlDataAdapter class in the System.Data.SqlClient namespace.

用户不会直接创建DataReader类的实例。Users do not create an instance of a DataReader class directly. 相反, 它们DataReader ExecuteReader通过Command对象的方法获取实例。Instead, they obtain the DataReader instance through the ExecuteReader method of the Command object. 因此, 应将构造DataReader函数标记为内部。Therefore, you should mark DataReader constructors as internal.

属性

Depth Depth Depth Depth

获取一个值,该值指示当前行的嵌套深度。Gets a value indicating the depth of nesting for the current row.

FieldCount FieldCount FieldCount FieldCount

获取当前行中的列数。Gets the number of columns in the current row.

(Inherited from IDataRecord)
IsClosed IsClosed IsClosed IsClosed

获取一个值,该值指示数据读取器是否已关闭。Gets a value indicating whether the data reader is closed.

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

获取位于指定索引处的列。Gets the column located at the specified index.

(Inherited from IDataRecord)
Item[String] Item[String] Item[String] Item[String]

获取具有指定名称的列。Gets the column with the specified name.

(Inherited from IDataRecord)
RecordsAffected RecordsAffected RecordsAffected RecordsAffected

通过执行 SQL 语句获取更改、插入或删除的行数。Gets the number of rows changed, inserted, or deleted by execution of the SQL statement.

方法

Close() Close() Close() Close()

关闭 IDataReader 对象。Closes the IDataReader Object.

Dispose() Dispose() Dispose() Dispose()

执行与释放或重置非托管资源关联的应用程序定义的任务。Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

(Inherited from IDisposable)
GetBoolean(Int32) GetBoolean(Int32) GetBoolean(Int32) GetBoolean(Int32)

获取指定列的布尔值形式的值。Gets the value of the specified column as a Boolean.

(Inherited from IDataRecord)
GetByte(Int32) GetByte(Int32) GetByte(Int32) GetByte(Int32)

获取指定列的 8 位无符号整数值。Gets the 8-bit unsigned integer value of the specified column.

(Inherited from IDataRecord)
GetBytes(Int32, Int64, Byte[], Int32, Int32) GetBytes(Int32, Int64, Byte[], Int32, Int32) GetBytes(Int32, Int64, Byte[], Int32, Int32) GetBytes(Int32, Int64, Byte[], Int32, Int32)

将字节流从指定列偏移量读入到缓冲区中,将其作为从给定缓冲区偏移量开始的数组。Reads a stream of bytes from the specified column offset into the buffer as an array, starting at the given buffer offset.

(Inherited from IDataRecord)
GetChar(Int32) GetChar(Int32) GetChar(Int32) GetChar(Int32)

获取指定列的字符值。Gets the character value of the specified column.

(Inherited from IDataRecord)
GetChars(Int32, Int64, Char[], Int32, Int32) GetChars(Int32, Int64, Char[], Int32, Int32) GetChars(Int32, Int64, Char[], Int32, Int32) GetChars(Int32, Int64, Char[], Int32, Int32)

将字符流从指定列偏移量读入到缓冲区中,将其作为从给定缓冲区偏移量开始的数组。Reads a stream of characters from the specified column offset into the buffer as an array, starting at the given buffer offset.

(Inherited from IDataRecord)
GetData(Int32) GetData(Int32) GetData(Int32) GetData(Int32)

返回指定列序号的 IDataReaderReturns an IDataReader for the specified column ordinal.

(Inherited from IDataRecord)
GetDataTypeName(Int32) GetDataTypeName(Int32) GetDataTypeName(Int32) GetDataTypeName(Int32)

获取指定字段的数据类型信息。Gets the data type information for the specified field.

(Inherited from IDataRecord)
GetDateTime(Int32) GetDateTime(Int32) GetDateTime(Int32) GetDateTime(Int32)

获取指定字段的日期和时间数据值。Gets the date and time data value of the specified field.

(Inherited from IDataRecord)
GetDecimal(Int32) GetDecimal(Int32) GetDecimal(Int32) GetDecimal(Int32)

获取指定字段的固定位置数值。Gets the fixed-position numeric value of the specified field.

(Inherited from IDataRecord)
GetDouble(Int32) GetDouble(Int32) GetDouble(Int32) GetDouble(Int32)

获取指定字段的双精度浮点数。Gets the double-precision floating point number of the specified field.

(Inherited from IDataRecord)
GetFieldType(Int32) GetFieldType(Int32) GetFieldType(Int32) GetFieldType(Int32)

获取对应于会从 GetValue(Int32) 返回的 Object 类型的 Type 信息。Gets the Type information corresponding to the type of Object that would be returned from GetValue(Int32).

(Inherited from IDataRecord)
GetFloat(Int32) GetFloat(Int32) GetFloat(Int32) GetFloat(Int32)

获取指定字段的单精度浮点数。Gets the single-precision floating point number of the specified field.

(Inherited from IDataRecord)
GetGuid(Int32) GetGuid(Int32) GetGuid(Int32) GetGuid(Int32)

返回指定字段的 GUID 值。Returns the GUID value of the specified field.

(Inherited from IDataRecord)
GetInt16(Int32) GetInt16(Int32) GetInt16(Int32) GetInt16(Int32)

获取指定字段的 16 位带符号整数值。Gets the 16-bit signed integer value of the specified field.

(Inherited from IDataRecord)
GetInt32(Int32) GetInt32(Int32) GetInt32(Int32) GetInt32(Int32)

获取指定字段的 32 位带符号整数值。Gets the 32-bit signed integer value of the specified field.

(Inherited from IDataRecord)
GetInt64(Int32) GetInt64(Int32) GetInt64(Int32) GetInt64(Int32)

获取指定字段的 64 位带符号整数值。Gets the 64-bit signed integer value of the specified field.

(Inherited from IDataRecord)
GetName(Int32) GetName(Int32) GetName(Int32) GetName(Int32)

获取要查找的字段的名称。Gets the name for the field to find.

(Inherited from IDataRecord)
GetOrdinal(String) GetOrdinal(String) GetOrdinal(String) GetOrdinal(String)

返回已命名字段的索引。Return the index of the named field.

(Inherited from IDataRecord)
GetSchemaTable() GetSchemaTable() GetSchemaTable() GetSchemaTable()

返回一个 DataTable,它描述 IDataReader 的列元数据。Returns a DataTable that describes the column metadata of the IDataReader.

GetString(Int32) GetString(Int32) GetString(Int32) GetString(Int32)

获取指定字段的字符串值。Gets the string value of the specified field.

(Inherited from IDataRecord)
GetValue(Int32) GetValue(Int32) GetValue(Int32) GetValue(Int32)

返回指定字段的值。Return the value of the specified field.

(Inherited from IDataRecord)
GetValues(Object[]) GetValues(Object[]) GetValues(Object[]) GetValues(Object[])

使用当前记录的列值填充对象的数组。Populates an array of objects with the column values of the current record.

(Inherited from IDataRecord)
IsDBNull(Int32) IsDBNull(Int32) IsDBNull(Int32) IsDBNull(Int32)

返回指定字段是否设置为 null。Return whether the specified field is set to null.

(Inherited from IDataRecord)
NextResult() NextResult() NextResult() NextResult()

在读取一批 SQL 语句的结果时,使数据读取器前进到下一个结果。Advances the data reader to the next result, when reading the results of batch SQL statements.

Read() Read() Read() Read()

IDataReader 前进到下一条记录。Advances the IDataReader to the next record.

适用于