共用方式為


IDataReader.Read 方法

定義

IDataReader 推進到下一筆記錄。

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

傳回

如果有多個資料列,則為 true;否則為 false

範例

下列範例會建立三個衍生類別 OleDbConnection、、 OleDbCommand和 的 OleDbDataReader實例。 此範例會讀取數據,並將其寫出至主控台。 最後,此範例會 OleDbDataReader關閉 ,然後關閉 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 的預設位置較第一筆記錄早。 因此,您必須呼叫 Read 以開始存取任何數據。

當資料讀取器正在使用中時,相關聯的連接正忙碌中提供 IDataReader。 這是直到呼叫為止 Close 的情況。

適用於