SqlDataReader.Close 方法


关闭 SqlDataReader 对象。Closes the SqlDataReader object.

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



下面的示例创建一个 SqlConnection 、一个 SqlCommand 和一个 SqlDataReaderThe following example creates a SqlConnection, a SqlCommand, and a SqlDataReader. 该示例将读取数据,并将其写出到控制台窗口。The example reads through the data, writing it out to the console window. 然后,该代码将关闭 SqlDataReaderThe code then closes the SqlDataReader. SqlConnection 代码块的末尾自动关闭 usingThe SqlConnection is closed automatically at the end of the using code block.

private static void ReadOrderData(string connectionString)
    string queryString =
        "SELECT OrderID, CustomerID FROM dbo.Orders;";

    using (SqlConnection connection =
               new SqlConnection(connectionString))

        using (SqlCommand command =
            new SqlCommand(queryString, connection))
            using (SqlDataReader reader = command.ExecuteReader())
                // Call Read before accessing data.
                while (reader.Read())
                    Console.WriteLine(String.Format("{0}, {1}",
                        reader[0], reader[1]));

                // Call Close when done reading.
Private Sub ReadOrderData(ByVal connectionString As String)
    Dim queryString As String = _
        "SELECT OrderID, CustomerID FROM dbo.Orders;"

    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)

        Dim reader As SqlDataReader = command.ExecuteReader()

        ' Call Read before accessing data.
        While reader.Read()
            Console.WriteLine(String.Format("{0}, {1}", _
                reader(0), reader(1)))
        End While

        ' Call Close when done reading.
    End Using
End Sub


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

Close方法填写输出参数的值、返回值和 RecordsAffected ,增加关闭 SqlDataReader 用于处理大型或复杂查询的的时间。The Close method fills in the values for output parameters, return values and RecordsAffected, increasing the time that it takes to close a SqlDataReader that was used to process a large or complex query. 如果返回值和查询影响的记录数不重要,则关闭该方法所用的时间 SqlDataReader 可以通过在 Cancel SqlCommand 调用方法之前调用关联对象的方法来减少 CloseWhen the return values and the number of records affected by a query are not significant, the time that it takes to close the SqlDataReader can be reduced by calling the Cancel method of the associated SqlCommand object before calling the Close method.


不要在 Close 类的 Dispose 方法中对连接、DataReader 或任何其他托管对象调用或 FinalizeDo 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.