IDataReader.Read Metodo

Definizione

Sposta l'oggetto IDataReader al record successivo.Advances the IDataReader to the next record.

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

Restituisce

true se sono presenti altre righe; in caso contrario, false.true if there are more rows; otherwise, false.

Esempi

Nell'esempio seguente vengono create istanze di tre classi derivate OleDbConnection, un OleDbCommande un OleDbDataReader.The following example creates instances of three derived classes OleDbConnection, an OleDbCommand, and an OleDbDataReader. L'esempio legge i dati e li scrive nella console.The example reads through the data, writing it out to the console. Infine, l'esempio chiude la OleDbDataReader, quindi l'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

Commenti

La posizione predefinita del IDataReader è precedente al primo record.The default position of the IDataReader is prior to the first record. È pertanto necessario chiamare Read per iniziare ad accedere ai dati.Therefore you must call Read to begin accessing any data.

Mentre il lettore dati è in uso, la connessione associata è occupata per soddisfare le IDataReader.While the data reader is in use, the associated connection is busy serving the IDataReader. Questo è il caso fino a quando non viene chiamato Close.This is the case until Close is called.

Si applica a