Export (0) Print
Expand All

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.

Namespace: System.Data
Assembly: System.Data (in system.data.dll)

'Declaration
Public Interface IDataReader
	Inherits IDisposable, IDataRecord
'Usage
Dim instance As IDataReader

public interface IDataReader extends IDisposable, IDataRecord
public interface IDataReader extends IDisposable, IDataRecord
Not applicable.

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 the DataReader. For more information about implementing .NET Framework data providers, see Implementing a .NET Framework Data Provider.

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.

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.

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

Windows 98, Windows Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft