IDataReader.Read 方法

定义

IDataReader 前进到下一条记录。

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

返回

如果存在更多行,则为 true;否则为 false

示例

以下示例创建三个OleDbCommand派生类OleDbConnection的实例, 和 OleDbDataReader。 该示例通读数据,将其写入控制台。 最后,该示例关闭 ,然后OleDbConnection关闭 OleDbDataReader

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 就是这种情况。

适用于