共用方式為


OleDbDataReader.Read 方法

定義

OleDbDataReader 推進到下一筆記錄。

public:
 override bool Read();
public:
 virtual bool Read();
public override bool Read ();
public bool Read ();
override this.Read : unit -> bool
abstract member Read : unit -> bool
override this.Read : unit -> bool
Public Overrides Function Read () As Boolean
Public Function Read () As Boolean

傳回

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

實作

範例

下列範例會建立 OleDbConnection、、 OleDbCommandOleDbDataReader。 此範例會讀取數據,並將它寫出主控台。 最後,此範例會 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

備註

的預設位置 OleDbDataReader 是在第一筆記錄之前。 因此,您必須呼叫 Read 以開始存取任何數據。

使用 時 OleDbDataReader ,相關聯的 OleDbConnection 會忙於提供服務,直到您呼叫 Close為止。

適用於

另請參閱