SqlDataReader.Close 메서드

정의

SqlDataReader 개체를 닫습니다.

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 ()

구현

예제

다음 예제에서는 , 및 를 SqlCommandSqlDataReader만듭니다SqlConnection. 이 예제에서는 데이터를 읽고 콘솔 창에 기록합니다. 그런 다음 코드는 를 SqlDataReader닫습니다. 는 SqlConnection 코드 블록의 using 끝에서 자동으로 닫힙니다.

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

설명

를 사용하여 다른 용도로 Close 연결된 SqlConnectionSqlDataReader 사용할 때 메서드를 명시적으로 호출해야 합니다.

메서드는 Close 출력 매개 변수, 반환 값 및 RecordsAffected에 대한 값을 채우며 큰 쿼리 또는 복잡한 쿼리를 처리하는 데 사용된 을 SqlDataReader 닫는 데 걸리는 시간을 늘립니다. 반환 값과 쿼리의 영향을 받는 레코드 수가 중요하지 않은 경우 메서드를 호출 Close 하기 전에 연결된 SqlCommand 개체의 메서드를 호출 Cancel 하여 를 닫 SqlDataReader 는 데 걸리는 시간을 줄일 수 있습니다.

주의

클래스의 메서드에서 Connection, DataReader 또는 다른 관리 개체에서 Finalize 또는 Dispose 를 호출 Close 하지 마세요. 종료자에서 클래스가 직접 소유하는 관리되지 않는 리소스만 해제해야 합니다. 클래스에 관리되지 않는 리소스가 없는 경우 클래스 정의에 Finalize 메서드를 포함하지 마세요. 자세한 내용은 가비지 수집을 참조하세요.

적용 대상

추가 정보