다음을 통해 공유


SqlDataReader.Close 메서드

SqlDataReader 개체를 닫습니다.

네임스페이스: System.Data.SqlClient
어셈블리: System.Data(system.data.dll)

구문

‘선언
Public Overrides Sub Close
‘사용 방법
Dim instance As SqlDataReader

instance.Close
public override void Close ()
public:
virtual void Close () override
public void Close ()
public override function Close ()

설명

연관된 SqlConnection을 다른 목적으로 사용하려면 SqlDataReader를 사용하여 작업을 완료할 때 Close 메서드를 명시적으로 호출해야 합니다.

Close 메서드는 출력 매개 변수 값을 채우고, 크거나 복잡한 쿼리를 처리하는 데 사용되는 SqlDataReader를 닫는 데 걸리는 시간을 증가시키는 값과 RecordsAffected를 반환합니다. 반환 값과 쿼리의 영향을 받는 레코드 수가 크지 않은 경우, SqlDataReader를 닫는 데 걸리는 시간은 Close 메서드를 호출하기 전에 관련 SqlCommand 개체의 Cancel 메서드를 호출하여 줄일 수 있습니다.

경고

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

예제

다음 예제에서는 SqlConnection, SqlCommand, SqlDataReader 등을 만듭니다. 이 예제에서는 데이터를 읽고 해당 데이터를 콘솔 창에 씁니다. 그런 다음 SqlDataReader를 닫습니다. SqlConnectionusing 코드 블록의 끝에서 자동으로 닫힙니다.

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
private static void ReadOrderData(string connectionString)
{
    string queryString =
        "SELECT OrderID, CustomerID FROM dbo.Orders;";

    using (SqlConnection connection =
               new SqlConnection(connectionString))
    {
        SqlCommand command =
            new SqlCommand(queryString, connection);
        connection.Open();

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

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

SqlDataReader 클래스
SqlDataReader 멤버
System.Data.SqlClient 네임스페이스

기타 리소스

ADO.NET에서 데이터 연결 및 검색
.NET Framework Data Provider for SQL Server 사용