SqlDataReader.Close 方法

定義

關閉 SqlDataReader 物件。Closes the SqlDataReader object.

public:
 virtual void Close();
public void Close ();
abstract member Close : unit -> unit
override this.Close : unit -> unit
Public 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 會在 using 程式碼區塊的結尾自動關閉。The 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))
    {
        connection.Open();

        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.
               reader.Close();
            }
        }
    }
}
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)
        connection.Open()

        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.
        reader.Close()
    End Using
End Sub

備註

當您使用 SqlDataReader 將相關聯的 SqlConnection 用於任何其他用途時,必須明確地呼叫 Close 方法。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 所需的時間,可以藉由在呼叫 Close 方法之前呼叫相關聯 SqlCommand 物件的 Cancel 方法來減少。When 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.

警告

請勿在連接、DataReader 或類別的 Finalize 方法中的任何其他 managed 物件上呼叫 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. 如果類別未擁有任何 Unmanaged 資源,請不要在類別定義中包含 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.

適用於

另請參閱