IDataReader 接口

提供一种方法来读取一个或多个通过在数据源执行命令所获得的只进结果集流,这是由访问关系数据库的 .NET Framework 数据提供程序实现的。

**命名空间:**System.Data
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public Interface IDataReader
    Inherits IDisposable, IDataRecord
用法
Dim instance As IDataReader
public interface IDataReader : IDisposable, IDataRecord
public interface class IDataReader : IDisposable, IDataRecord
public interface IDataReader extends IDisposable, IDataRecord
public interface IDataReader extends IDisposable, IDataRecord

备注

IDataReaderIDataRecord 接口允许继承类实现 DataReader 类,提供一种读取一个或多个结果集只进流的方法。有关 DataReader 类的更多信息,请参见 使用 DataReader 检索数据。有关实现 .NET Framework 数据提供程序的更多信息,请参见 Implementing a .NET Framework Data Provider

应用程序不直接创建 IDataReader 接口的实例,而是创建继承 IDataReader 的类的实例。

继承 IDataReader 的类必须实现继承的成员,并通常会定义附加成员来添加提供程序特定的功能。

在读取数据的过程中,实现 IDataReader 的类的用户可以看见其他进程或线程对结果集所做的更改。但是,精确的行为既取决于提供程序又取决于执行时间。

给实现者的说明 为提高 .NET Framework 数据提供程序之间的一致性,请命名 Prv Command 窗体中的继承类,其中 Prv 是为特定 .NET Framework 数据提供程序命名空间中的所有类分配的统一前缀。例如,Sql 是 System.Data.SqlClient 命名空间中 SqlDataAdapter 类的前缀。 用户并不直接创建 DataReader 类的实例。相反,他们从 Command 对象的 ExecuteReader 方法获取 DataReader。因此,您应该将 DataReader 构造函数标记为内部构造函数。

示例

下面的示例创建派生类 SqlConnectionSqlCommandSqlDataReader 的实例。该示例读取全部数据,并将这些数据写到控制台。最后,该示例先关闭 SqlDataReader,然后关闭 SqlConnection

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()
            Console.WriteLine(String.Format("{0}, {1}", _
                reader(0), reader(1)))
        End While

        ' Call Close when done reading.
        reader.Close()
    End Using
End Sub
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())
        {
            Console.WriteLine(String.Format("{0}, {1}",
                reader[0], reader[1]));
        }

        // Call Close when done reading.
        reader.Close();
    }
}

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

IDataReader 成员
System.Data 命名空间