IDataReader.Read 方法

定义

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

public:
 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 OleDbCommand的实例: 和OleDbDataReaderThe 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, OleDbConnection然后关闭。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);
        connection.Open();
        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.
        reader.Close();
    }
}
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)
        connection.Open()

        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.
        reader.Close()
    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. 在调用之前Close , 就是这种情况。This is the case until Close is called.

适用于