This documentation is archived and is not being maintained.

OdbcDataReader.Read Method

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

Advances the OdbcDataReader to the next record.

[Visual Basic]
Public Overridable Function Read() As Boolean Implements _
   IDataReader.Read
[C#]
public virtual bool Read();
[C++]
public: virtual bool Read();
[JScript]
public function Read() : Boolean;

Return Value

true if there are more rows; otherwise, false.

Implements

IDataReader.Read

Remarks

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

While the OdbcDataReader is in use, the associated OdbcConnection is busy serving it until you call Close.

Example

[Visual Basic, C#, C++] The following example creates an OdbcConnection, an OdbcCommand, and an OdbcDataReader. The example reads through the data, writing it out to the console. Finally, the example closes the OdbcDataReader, then the OdbcConnection.

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

[C#] 
public void ReadMyData(string myConnString) 
{
   string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
   OleDbConnection myConnection = new OleDbConnection(myConnString);
   OleDbCommand myCommand = new OleDbCommand(mySelectQuery,myConnection);
   myConnection.Open();
   OleDbDataReader 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";
    OleDbConnection* myConnection = new OleDbConnection(myConnString);
    OleDbCommand* myCommand = new OleDbCommand(mySelectQuery,myConnection);
    myConnection->Open();
    OleDbDataReader* 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

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

See Also

OdbcDataReader Class | OdbcDataReader Members | System.Data.Odbc Namespace

Show: