OleDbDataReader.Close 方法

定义

关闭 OleDbDataReader 对象。

public:
 override void Close();
public:
 virtual void Close();
public override void Close ();
public void Close ();
override this.Close : unit -> unit
abstract member Close : unit -> unit
override this.Close : unit -> unit
Public Overrides Sub Close ()
Public Sub Close ()

实现

示例

以下示例创建 OleDbConnectionOleDbCommandOleDbDataReader。 该示例读取数据,将其写出到控制台。 最后,该示例关闭 , 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

注解

使用 时,必须显式调用 Close 方法,以便将 OleDbDataReader 关联的 OleDbConnection 用于任何其他目的。

注意

不要在类的 Finalize 方法中对 Connection、DataReader 或任何其他托管对象调用 CloseDispose。 在终结器中,应仅释放类直接拥有的非托管资源。 如果类不拥有任何非托管资源,则不要在类定义中包含 Finalize 方法。 有关详细信息,请参阅垃圾回收

适用于

另请参阅