IDataReader.Read 方法


IDataReader 前进到下一条记录。Advances the IDataReader to the next record.

 bool Read();
public bool Read ();
abstract member Read : unit -> bool
Public Function Read () As Boolean



如果存在更多行,则为 true;否则为 falsetrue if there are more rows; otherwise, false.


下面的示例创建三个派生类的实例: OleDbConnection OleDbCommandOleDbDataReaderThe 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. 最后,该示例关闭 OleDbDataReader ,然后关闭 OleDbConnectionFinally, 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.
Public Sub ReadData(ByVal connectionString As String)
    Dim queryString As String = _
        "SELECT OrderID, CustomerID FROM Orders"

    Using connection As New OracleConnection(connectionString)
        Dim command As New OracleCommand(queryString, connection)

        Dim reader As OracleDataReader
        reader = command.ExecuteReader()

        ' Always call Read before accessing data.
        While reader.Read()
            Console.WriteLine(reader.GetInt32(0) & ", " & reader.GetString(1))
        End While

        ' Always call Close when done reading.
    End Using
End Sub


IDataReader 的默认位置在第一个记录之前。The default position of the IDataReader is prior to the first record. 因此,您必须调用 Read 以开始访问任何数据。Therefore you must call Read to begin accessing any data.

当数据读取器正在使用时,关联的连接正在忙于提供 IDataReaderWhile the data reader is in use, the associated connection is busy serving the IDataReader. 在调用之前,就是这种情况 CloseThis is the case until Close is called.