OleDbDataReader.Close 方法

定义

关闭 OleDbDataReader 对象。Closes the OleDbDataReader object.

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

示例

下面的示例创建 OleDbConnectionOleDbCommandOleDbDataReaderThe following example creates an OleDbConnection, an OleDbCommand, and an OleDbDataReader. 该示例将读取数据,并将其写出到控制台。The example reads through the data, writing it out to the console. 最后,该示例关闭 OleDbDataReader,然后 OleDbConnectionFinally, the example closes the OleDbDataReader, and 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

注解

使用 OleDbDataReader 将关联的 OleDbConnection 用于任何其他用途时,必须显式调用 Close 方法。You must explicitly call the Close method when you are through using the OleDbDataReader to use the associated OleDbConnection for any other purpose.

注意

不要对类的Finalize方法中的连接、DataReader 或任何其他托管对象调用CloseDispose操作。Do not call Close or Dispose on a Connection, a DataReader, or any other managed object in the Finalize method of your class. 在终结器中,只应释放类直接拥有的非托管资源。In a finalizer, you should only release unmanaged resources that your class owns directly. 如果类不拥有任何非托管资源,则不要在类定义中包括Finalize方法。If your class does not own any unmanaged resources, do not include a Finalize method in your class definition. 有关详细信息,请参阅垃圾回收For more information, see Garbage Collection.

适用于

另请参阅