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.

For a list of all members of this type, see IDataReader Members.

[Visual Basic]
Public Interface IDataReader
   Inherits IDisposable, IDataRecord
[C#]
public interface IDataReader : IDisposable, IDataRecord
[C++]
public __gc __interface IDataReader : public IDisposable,
   IDataRecord
[JScript]
public interface IDataReader implements IDisposable, IDataRecord

Classes that Implement IDataReader

Class Description
OdbcDataReader Provides a way of reading a forward-only stream of data rows from a data source. This class cannot be inherited.
OleDbDataReader Provides a way of reading a forward-only stream of data rows from a data source. This class cannot be inherited.
OracleDataReader Provides a way of reading a forward-only stream of data rows from a data source. This class cannot be inherited.
SqlCeDataReader Provides a way of reading a forward-only stream of data rows from a data source. This class cannot be inherited.
SqlDataReader Provides a means of reading a forward-only stream of rows from a SQL Server database. This class cannot be inherited.

Remarks

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 resultset 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 through the ExecuteReader method of the Command object. Therefore, you should mark DataReader constructors as internal.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Public Sub ReadMyData(myConnString As String)
    Dim mySelectQuery As String = "SELECT OrderID, CustomerID FROM Orders"
    Dim myConnection As New SqlConnection(myConnString)
    Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
    myConnection.Open()
    Dim myReader As SqlDataReader
    myReader = myCommand.ExecuteReader()
    ' Always call Read before accessing data.
    While myReader.Read()
        Console.WriteLine((myReader.GetInt32(0) & ", " & myReader.GetString(1)))
    End While
    ' always call Close when done reading.
    myReader.Close()
    ' Close the connection when done with it.
    myConnection.Close()
End Sub 'ReadMyData

[C#] 
public void ReadMyData(string myConnString) {
    string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
    SqlConnection myConnection = new SqlConnection(myConnString);
    SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
    myConnection.Open();
    SqlDataReader myReader;
    myReader = myCommand.ExecuteReader();
    // Always call Read before accessing data.
    while (myReader.Read()) {
       Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
    }
    // always call Close when done reading.
    myReader.Close();
    // Close the connection when done with it.
    myConnection.Close();
 }

[C++] 
public:
void ReadMyData(String* myConnString) {
    String* mySelectQuery = S"SELECT OrderID, CustomerID FROM Orders";
    SqlConnection* myConnection = new SqlConnection(myConnString);
    SqlCommand* myCommand = new SqlCommand(mySelectQuery,myConnection);
    myConnection->Open();
    SqlDataReader* myReader;
    myReader = myCommand->ExecuteReader();
    // Always call Read before accessing data.
    while (myReader->Read()) {
       Console::WriteLine(S"{0}, {1}", __box(myReader->GetInt32(0)), myReader->GetString(1));
    }
    // always call Close when done reading.
    myReader->Close();
    // Close the connection when done with it.
    myConnection->Close();
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Data

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Data (in System.Data.dll)

See Also

IDataReader Members | System.Data Namespace

Show:
© 2014 Microsoft