IDataReader.Read Método

Definición

Desplaza IDataReader al siguiente registro.Advances the IDataReader to the next record.

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

Devoluciones

Boolean

true si hay más filas; de lo contrario, false.true if there are more rows; otherwise, false.

Ejemplos

En el ejemplo siguiente se crean instancias de tres clases derivadas OleDbConnection , OleDbCommand y OleDbDataReader .The following example creates instances of three derived classes OleDbConnection, an OleDbCommand, and an OleDbDataReader. En el ejemplo se leen los datos y se escriben en la consola.The example reads through the data, writing it out to the console. Finalmente, el ejemplo cierra y, OleDbDataReader a continuación, el 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

Comentarios

La posición predeterminada de la clase IDataReader es anterior al primer registro.The default position of the IDataReader is prior to the first record. Por lo tanto, debe llamar Read a para comenzar a acceder a los datos.Therefore you must call Read to begin accessing any data.

Mientras el lector de datos está en uso, la conexión asociada está ocupada atendiendo a IDataReader .While the data reader is in use, the associated connection is busy serving the IDataReader. Este es el caso hasta que Close se llama a.This is the case until Close is called.

Se aplica a