다음을 통해 공유


SqlPipe 개체

적용 대상: SQL Server

이전 버전의 SQL Server에서는 결과 또는 출력 매개 변수를 호출 클라이언트로 보내는 저장 프로시저(또는 확장 저장 프로시저)를 작성하는 것이 매우 일반적입니다.

Transact-SQL 저장 프로시저 에서 0개 이상의 행을 반환하는 SELECT 문은 연결된 호출자의 "파이프"에 결과를 보냅니다.

SQL Server에서 실행되는 CLR(공용 언어 런타임) 데이터베이스 개체의 경우 SqlPipe 개체의 Send 메서드를 사용하여 연결된 파이프로 결과를 보낼 수 있습니다. SqlContext 개체의 Pipe 속성에 액세스하여 SqlPipe 개체를 가져옵니다. SqlPipe 클래스는 개념적으로 ASP.NET 있는 Response 클래스와 비슷합니다. 자세한 내용은 .NET Framework 소프트웨어 개발 키트의 SqlPipe 클래스 참조 설명서를 참조하세요.

테이블 형식 결과 및 메시지 반환

SqlPipe 에는 3개의 오버로드가 있는 Send 메서드가 있습니다. 화면은 다음과 같습니다.

  • void Send(string message)

  • void Send(SqlDataReader reader)

  • void Send(SqlDataRecord record)

합니다 보낼 메서드는 클라이언트 또는 호출자에 게 직접 데이터를 보냅니다. 일반적으로 SqlPipe출력을 사용하는 클라이언트이지만 중첩된 CLR 저장 프로시저의 경우 출력 소비자도 저장 프로시저가 될 수 있습니다. 예를 들어 Procedure1은 명령 텍스트 "EXEC Procedure2"를 사용하여 SqlCommand.ExecuteReader()를 호출합니다. Procedure2도 관리되는 저장 프로시저입니다. Procedure2가 이제 SqlPipe.Send(SqlDataRecord)를 호출하면 행이 클라이언트가 아닌 Procedure1의 판독기로 전송됩니다.

Send 메서드는 Transact-SQL의 PRINT와 동일한 정보 메시지로 클라이언트에 표시되는 문자열 메시지를 보냅니다. SqlDataRecord를 사용하여 단일 행 결과 집합을 보내거나 SqlDataReader를 사용하여 다중 행 결과 집합을 보낼 수도 있습니다.

SqlPipe 개체에는 ExecuteAndSend 메서드도 있습니다. 이 메서드를 사용하여 명령을 실행하고(SqlCommand 개체로 전달됨) 결과를 호출자에게 직접 보낼 수 있습니다. 제출된 명령에 오류가 있는 경우 예외가 파이프로 전송되지만 복사본도 호출 관리 코드로 전송됩니다. 호출 코드가 예외를 catch하지 않으면 스택을 Transact-SQL 코드로 전파하고 출력에 두 번 나타납니다. 호출 코드가 예외를 catch하는 경우 파이프 소비자는 여전히 오류를 볼 수 있지만 중복 오류는 없습니다.

컨텍스트 연결과 연결된 SqlCommand만 사용할 수 있으며 비 컨텍스트 연결과 연결된 명령을 사용할 수 없습니다.

사용자 지정 결과 집합 반환

관리되는 저장 프로시저는 SqlDataReader에서 제공되지 않은 결과 집합을 보낼 수 있습니다. SendResultsStart 메서드와 SendResultsRowSendResultsEnd를 사용하면 저장 프로시저가 사용자 지정 결과 집합을 클라이언트로 보낼 수 있습니다.

SendResultsStartSqlDataRecord 를 입력으로 사용합니다. 결과 집합의 시작을 표시하고 레코드 메타데이터를 사용하여 결과 집합을 설명하는 메타데이터를 생성합니다. SendResultsStart를 사용하여 레코드 값을 보내지 않습니다. SendResultsRow를 사용하여 보낸 이후의 모든 행은 이 메타데이터 정의와 일치해야 합니다.

참고 항목

SendResultsStart 메서드를 호출한 후에는 SendResultsRowSendResultsEnd 만 호출할 수 있습니다. 동일한 SqlPipe 인스턴스에서 다른 메서드를 호출하면 InvalidOperationException발생합니다. SendResultsEnd는 SqlPipe를 다른 메서드를 호출할 수 있는 초기 상태로 다시 설정합니다.

예시

uspGetProductLine 저장 프로시저는 지정된 제품 라인 내의 모든 제품의 이름, 제품 번호, 색 및 정가를 반환합니다. 이 저장 프로시저는 prodLine에 대한 정확한 일치를 허용합니다.

C#

using System;  
using System.Data;  
using System.Data.SqlClient;  
using System.Data.SqlTypes;  
using Microsoft.SqlServer.Server;  
  
public partial class StoredProcedures  
{  
[Microsoft.SqlServer.Server.SqlProcedure]  
public static void uspGetProductLine(SqlString prodLine)  
{  
    // Connect through the context connection.  
    using (SqlConnection connection = new SqlConnection("context connection=true"))  
    {  
        connection.Open();  
  
        SqlCommand command = new SqlCommand(  
            "SELECT Name, ProductNumber, Color, ListPrice " +  
            "FROM Production.Product " +   
            "WHERE ProductLine = @prodLine;", connection);  
  
        command.Parameters.AddWithValue("@prodLine", prodLine);  
  
        try  
        {  
            // Execute the command and send the results to the caller.  
            SqlContext.Pipe.ExecuteAndSend(command);  
        }  
        catch (System.Data.SqlClient.SqlException ex)  
        {  
            // An error occurred executing the SQL command.  
        }  
     }  
}  
};  

Visual Basic

Imports System  
Imports System.Data  
Imports System.Data.SqlClient  
Imports System.Data.SqlTypes  
Imports Microsoft.SqlServer.Server  
  
Partial Public Class StoredProcedures  
<Microsoft.SqlServer.Server.SqlProcedure()> _  
Public Shared Sub uspGetProductLine(ByVal prodLine As SqlString)  
    Dim command As SqlCommand  
  
    ' Connect through the context connection.  
    Using connection As New SqlConnection("context connection=true")  
        connection.Open()  
  
        command = New SqlCommand( _  
        "SELECT Name, ProductNumber, Color, ListPrice " & _  
        "FROM Production.Product " & _  
        "WHERE ProductLine = @prodLine;", connection)  
        command.Parameters.AddWithValue("@prodLine", prodLine)  
  
        Try  
            ' Execute the command and send the results   
            ' directly to the caller.  
            SqlContext.Pipe.ExecuteAndSend(command)  
        Catch ex As System.Data.SqlClient.SqlException  
            ' An error occurred executing the SQL command.  
        End Try  
    End Using  
End Sub  
End Class  

다음 Transact-SQL 문은 둘러보기 자전거 제품 목록을 반환하는 uspGetProduct 프로시저를 실행합니다.

EXEC uspGetProductLineVB 'T';  

참고 항목

SqlDataRecord 개체
CLR 저장 프로시저
ADO.NET SQL Server In-Process 특정 확장