SqlCommand.ExecuteReader 메서드

정의

CommandTextConnection에 보내고, SqlDataReader를 빌드합니다.

오버로드

ExecuteReader()

CommandTextConnection에 보내고, SqlDataReader를 빌드합니다.

ExecuteReader(CommandBehavior)

CommandTextConnection에 보내고, CommandBehavior 값 중 하나를 사용하여 SqlDataReader를 빌드합니다.

ExecuteReader()

CommandTextConnection에 보내고, SqlDataReader를 빌드합니다.

public:
 System::Data::SqlClient::SqlDataReader ^ ExecuteReader();
public System.Data.SqlClient.SqlDataReader ExecuteReader ();
override this.ExecuteReader : unit -> System.Data.SqlClient.SqlDataReader
member this.ExecuteReader : unit -> System.Data.SqlClient.SqlDataReader
Public Function ExecuteReader () As SqlDataReader

반환

SqlDataReader 개체입니다.

예외

SqlDbType 또는 VarBinary 이외의 는 로 설정Stream되었을 때 Value 사용되었습니다. 스트리밍에 대한 자세한 내용은 SqlClient 스트리밍 지원을 참조하세요.

또는

SqlDbType 로 설정된 TextReader경우 ValueChar, NChar, NVarChar, VarChar 또는 Xml 이외의 가 사용되었습니다.

또는

이 로 SqlDbType 설정XmlReader되었을 때 ValueXml 이외의 가 사용되었습니다.

잠긴 행에 대해 명령을 실행하는 동안 예외가 발생했습니다. Microsoft .NET Framework 버전 1.0을 사용하는 경우에는 이 예외가 생성되지 않습니다.

또는

스트리밍 작업 동안 시간이 초과되었습니다. 스트리밍에 대한 자세한 내용은 SqlClient 스트리밍 지원을 참조하세요.

연결의 현재 상태가 닫힘입니다. ExecuteReader()에 열린 SqlConnection이 필요합니다.

또는

스트리밍 작업 동안 SqlConnection이 닫히거나 삭제되었습니다. 스트리밍에 대한 자세한 내용은 SqlClient 스트리밍 지원을 참조하세요.

스트리밍 작업 동안 Stream, XmlReader 또는 TextReader 개체에서 오류가 발생했습니다. 스트리밍에 대한 자세한 내용은 SqlClient 스트리밍 지원을 참조하세요.

스트리밍 작업 동안 Stream, XmlReader 또는 TextReader 개체가 닫혔습니다. 스트리밍에 대한 자세한 내용은 SqlClient 스트리밍 지원을 참조하세요.

예제

다음 예제에서는 을 SqlCommand만든 다음 Transact-SQL SELECT 문인 문자열과 데이터 원본에 연결하는 데 사용할 문자열을 전달하여 실행합니다.

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        connection.Open();

        SqlCommand command = new SqlCommand(queryString, connection);
        using(SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}", reader[0]));
            }
        }
    }
}
Public Sub CreateCommand(ByVal queryString As String, _
  ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        connection.Open()

        Dim command As New SqlCommand(queryString, connection)
        Dim reader As SqlDataReader = command.ExecuteReader()
        Try
            While reader.Read()
                Console.WriteLine("{0}", reader(0))
            End While
        Finally
            ' Always call Close when done reading.
            reader.Close()
        End Try
    End Using
End Sub

설명

속성이 로 CommandTypeCommandText 설정StoredProcedure되면 속성을 저장 프로시저의 이름으로 설정해야 합니다. 명령은 를 호출 ExecuteReader할 때 이 저장 프로시저를 실행합니다.

참고

트랜잭션이 교착 상태이면 가 호출될 때까지 Read 예외가 throw되지 않을 수 있습니다.

MARS(다중 활성 결과 집합) 기능을 사용하면 동일한 연결을 사용하여 여러 작업을 수행할 수 있습니다.

또는 BeginExecuteReader 를 사용하여 ExecuteReader XML 데이터에 액세스하는 경우 SQL Server 각각 2,033자 행의 길이가 2,033자보다 큰 XML 결과를 반환합니다. 이 동작을 방지하려면 또는 BeginExecuteXmlReader 를 사용하여 ExecuteXmlReader FOR XML 쿼리를 읽습니다.

추가 정보

적용 대상

ExecuteReader(CommandBehavior)

CommandTextConnection에 보내고, CommandBehavior 값 중 하나를 사용하여 SqlDataReader를 빌드합니다.

public:
 System::Data::SqlClient::SqlDataReader ^ ExecuteReader(System::Data::CommandBehavior behavior);
public System.Data.SqlClient.SqlDataReader ExecuteReader (System.Data.CommandBehavior behavior);
override this.ExecuteReader : System.Data.CommandBehavior -> System.Data.SqlClient.SqlDataReader
member this.ExecuteReader : System.Data.CommandBehavior -> System.Data.SqlClient.SqlDataReader
Public Function ExecuteReader (behavior As CommandBehavior) As SqlDataReader

매개 변수

behavior
CommandBehavior

CommandBehavior 값 중 하나입니다.

반환

SqlDataReader 개체입니다.

예외

SqlDbType 또는 VarBinary 이외의 는 로 설정Stream되었을 때 Value 사용되었습니다. 스트리밍에 대한 자세한 내용은 SqlClient 스트리밍 지원을 참조하세요.

또는

SqlDbType 로 설정된 TextReader경우 ValueChar, NChar, NVarChar, VarChar 또는 Xml 이외의 가 사용되었습니다.

또는

이 로 SqlDbType 설정XmlReader되었을 때 ValueXml 이외의 가 사용되었습니다.

스트리밍 작업 동안 시간이 초과되었습니다. 스트리밍에 대한 자세한 내용은 SqlClient 스트리밍 지원을 참조하세요.

스트리밍 작업 동안 Stream, XmlReader 또는 TextReader 개체에서 오류가 발생했습니다. 스트리밍에 대한 자세한 내용은 SqlClient 스트리밍 지원을 참조하세요.

스트리밍 작업 동안 SqlConnection이 닫히거나 삭제되었습니다. 스트리밍에 대한 자세한 내용은 SqlClient 스트리밍 지원을 참조하세요.

스트리밍 작업 동안 Stream, XmlReader 또는 TextReader 개체가 닫혔습니다. 스트리밍에 대한 자세한 내용은 SqlClient 스트리밍 지원을 참조하세요.

예제

다음 예제에서는 을 SqlCommand만든 다음 Transact-SQL SELECT 문인 문자열과 데이터 원본에 연결하는 데 사용할 문자열을 전달하여 실행합니다. CommandBehaviorCloseConnection로 설정됩니다.

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        connection.Open();
        using(SqlDataReader reader =
            command.ExecuteReader(CommandBehavior.CloseConnection))
        {
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}", reader[0]));
            }
        }
    }
}
Public Sub CreateCommand(ByVal queryString As String, _
  ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)
        connection.Open()
        Dim reader As SqlDataReader = _
            command.ExecuteReader(CommandBehavior.CloseConnection)
        Try
            While reader.Read()
                Console.WriteLine("{0}", reader(0))
            End While
        Finally
            ' Always call Close when done reading.
            reader.Close()
        End Try
    End Using
End Sub

설명

속성이 로 CommandTypeCommandText 설정StoredProcedure되면 속성을 저장 프로시저의 이름으로 설정해야 합니다. 명령은 를 호출 ExecuteReader할 때 이 저장 프로시저를 실행합니다.

참고

를 사용하여 SequentialAccess 큰 값 및 이진 데이터를 검색합니다. 그렇지 않으면 가 OutOfMemoryException 발생할 수 있으며 연결이 닫힙니다.

MARS(다중 활성 결과 집합) 기능을 사용하면 동일한 연결을 사용하여 여러 작업을 수행할 수 있습니다.

또는 BeginExecuteReader 를 사용하여 ExecuteReader XML 데이터에 액세스하는 경우 SQL Server 각각 2,033자 행의 길이가 2,033자보다 큰 XML 결과를 반환합니다. 이 동작을 방지하려면 또는 BeginExecuteXmlReader 를 사용하여 ExecuteXmlReader FOR XML 쿼리를 읽습니다.

추가 정보

적용 대상