This documentation is archived and is not being maintained.

SqlDataReader Class

Provides a means of reading a forward-only stream of rows from a SQL Server database. This class cannot be inherited.

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


[Visual Basic]
NotInheritable Public Class SqlDataReader
   Inherits MarshalByRefObject
   Implements IEnumerable, IDataReader, IDisposable, IDataRecord
public sealed class SqlDataReader : MarshalByRefObject,
   IEnumerable, IDataReader, IDisposable, IDataRecord
public __gc __sealed class SqlDataReader : public
   MarshalByRefObject, IEnumerable, IDataReader, IDisposable,
public class SqlDataReader extends MarshalByRefObject implements
   IEnumerable, IDataReader, IDisposable, IDataRecord

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


To create a SqlDataReader, you must call the ExecuteReader method of the SqlCommand object, rather than directly using a constructor.

While the SqlDataReader is in use, the associated SqlConnection is busy serving the SqlDataReader, and no other operations can be performed on the SqlConnection other than closing it. This is the case until the Close method of the SqlDataReader is called. For example, you cannot retrieve output parameters until after you call Close.

Changes made to a resultset by another process or thread while data is being read may be visible to the user of the SqlDataReader. However, the precise behavior is timing dependent.

IsClosed and RecordsAffected are the only properties that you can call after the SqlDataReader is closed. Though the RecordsAffected property may be accessed at any time while the SqlDataReader exists, always call Close before returning the value of RecordsAffected to ensure an accurate return value.

Note   For optimal performance, SqlDataReader avoids creating unnecessary objects or making unnecessary copies of data. As a result, multiple calls to methods such as GetValue return a reference to the same object. Use caution if you are modifying the underlying value of the objects returned by methods such as GetValue.


[Visual Basic, C#, C++] The following example creates a SqlConnection, a SqlCommand, and a 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)
    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.
    ' Close the connection when done with it.
End Sub 'ReadMyData

public void ReadMyData(string myConnString) {
    string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
    SqlConnection myConnection = new SqlConnection(myConnString);
    SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
    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.
    // Close the connection when done with it.

void ReadMyData(String* myConnString) {
    String* mySelectQuery = S"SELECT OrderID, CustomerID FROM Orders";
    SqlConnection* myConnection = new SqlConnection(myConnString);
    SqlCommand* myCommand = new SqlCommand(mySelectQuery,myConnection);
    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.
    // Close the connection when done with it.

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


Namespace: System.Data.SqlClient

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

SqlDataReader Members | System.Data.SqlClient Namespace