OdbcDataReader.Close 方法


关闭 OdbcDataReader 对象。Closes the OdbcDataReader object.

 override void Close();
 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 ()



下面的示例创建 OdbcConnectionOdbcCommandOdbcDataReaderThe following example creates an OdbcConnection, an OdbcCommand, and an OdbcDataReader. 该示例将读取数据,并将其写出到控制台。The example reads through the data, writing it out to the console. 最后,该示例关闭 OdbcDataReader,然后 OdbcConnectionFinally, the example closes the OdbcDataReader, and then the OdbcConnection.

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);
        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.
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)

        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.
    End Using
End Sub


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


请勿对类的 Finalize 方法中的连接、DataReader 或任何其他托管对象调用 CloseDisposeDo 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.