This documentation is archived and is not being maintained.

SqlDataReader.Read Method

Advances the SqlDataReader to the next record.

[Visual Basic]
Public Overridable Function Read() As Boolean Implements _
public virtual bool Read();
public: virtual bool Read();
public function Read() : Boolean;

Return Value

true if there are more rows; otherwise, false.




The default position of the SqlDataReader is prior to the first record. Therefore, you must call Read to begin accessing any data.

Only one SqlDataReader per associated SqlConnection may be open at a time, and any attempt to open another will fail until the first one is closed. Similarly, while the SqlDataReader is in use, the associated SqlConnection is busy serving it until you call Close.


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


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

See Also

SqlDataReader Class | SqlDataReader Members | System.Data.SqlClient Namespace