Read Method
Collapse the table of content
Expand the table of content

IDataReader.Read Method ()


Advances the IDataReader to the next record.

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

bool Read()

Return Value

Type: System.Boolean

true if there are more rows; otherwise, false.

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

While the data reader is in use, the associated connection is busy serving the IDataReader. This is the case until Close is called.

The following example creates instances of three derived classes OleDbConnection, an OleDbCommand, and an OleDbDataReader. The example reads through the data, writing it out to the console. Finally, the example closes the OleDbDataReader, then the OleDbConnection.

private static void ReadData(string connectionString)
    string queryString = "SELECT OrderID, CustomerID FROM Orders";
    using (OracleConnection connection = new OracleConnection(connectionString))
        OracleCommand command = new OracleCommand(queryString, connection);
        OracleDataReader reader;
        reader = command.ExecuteReader();

        // Always call Read before accessing data.
        while (reader.Read())
            Console.WriteLine(reader.GetInt32(0) + ", " + reader.GetString(1));

        // Always call Close when done reading.

.NET Framework
Available since 1.1
Return to top
© 2015 Microsoft