IDataReader IDataReader IDataReader IDataReader Interface


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


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. 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";

    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);

            SqlDataReader reader = command.ExecuteReader();

            // Call Read before accessing data.
            while (reader.Read())

            // Call Close when done reading.

    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;"
    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)

            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.
        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


The 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. For more information about DataReader classes, see Retrieving Data Using a DataReader.

An application does not create an instance of the IDataReader interface directly, but creates an instance of a class that inherits IDataReader.

Classes that inherit IDataReader must implement the inherited members, and typically define additional members to add provider-specific functionality.

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.

Notes to Implementers

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. For example, Sql is the prefix of the SqlDataAdapter class in the System.Data.SqlClient namespace.

Users do not create an instance of a DataReader class directly. Instead, they obtain the DataReader instance through the ExecuteReader method of the Command object. Therefore, you should mark DataReader constructors as internal.


Depth Depth Depth Depth

Gets a value indicating the depth of nesting for the current row.

IsClosed IsClosed IsClosed IsClosed

Gets a value indicating whether the data reader is closed.

RecordsAffected RecordsAffected RecordsAffected RecordsAffected

Gets the number of rows changed, inserted, or deleted by execution of the SQL statement.


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

Closes the IDataReader Object.

GetSchemaTable() GetSchemaTable() GetSchemaTable() GetSchemaTable()

Returns a DataTable that describes the column metadata of the IDataReader.

NextResult() NextResult() NextResult() NextResult()

Advances the data reader to the next result, when reading the results of batch SQL statements.

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

Advances the IDataReader to the next record.

Applies to