Aracılığıyla paylaş


SqlPipe nesne

Önceki sürümlerinde SQL Server, Yaz bir saklı yordam (veya bir genişletilmiş saklı yordam) sonuçlar veya çıkış parametreleri çağıran istemci gönderir çok yaygın olduğu

İçinde bir Transact-SQL saklı yordam, tüm SELECT ifade sıfır veya daha fazla satır döndürür gönderir sonuçlara bağlı yapanın "boru."

İçin ortak dil çalışma zamanı (clr) veritabanı çalışan nesneler SQL Server, sonuçlar bağlı kanal kullanarak göndermek Send yöntemleri SqlPipe nesne.Erişim Pipe özellik SqlContext elde etmek için nesne SqlPipe nesne.The SqlPipe class is conceptually similar to the Response class found in ASP.NET.Daha fazla bilgi için SqlPipe sınıfı başvuru belgelerine bakın..NET Framework Yazılım Geliştirme Seti.

Sekmeli sonuçları ve iletileri döndüren

The SqlPipe has a Send method, which has three overloads.Bunlar:

  • void Send(string message)

  • void Send(SqlDataReader reader)

  • void Send(SqlDataRecord record)

The Send method sends data straight to the client or caller.Genellikle çıktısı tüketir istemci olur SqlPipe, ancak iç içe clr olması halinde saklı yordams çıktı tüketici de olabilir bir saklı yordam.Örneğin, Procedure1, SqlCommand.ExecuteReader() ile komut metni "exec procedure2" çağırır.Procedure2 ise de yönetilen bir saklı yordam.Procedure2 şimdi SqlPipe.Send (SqlDataRecord) çağırırsa, Procedure1'ın Okuyucu, istemci satırı gönderilir.

The Send method sends a string message that appears on the client as an information message, equivalent to PRINT in Transact-SQL.Bir tek sonuç küme kullanarak satır gönderebilirsiniz SqlDataRecord, veya sonuç küme kullanarak multi-row bir SqlDataReader.

The SqlPipe object also has an ExecuteAndSend method.Bu yöntem kullanılabilir yürütmek bir komut (olarak geçirilen bir SqlCommand nesnesi) ve sonuçlar doğrudan geri arayan için Gönder.Gönderildiği komutunda hata varsa kanala gönderilen özel durumlar, ancak bir kopyasını da arama yönetilen kod için gönderilir.Çağıran kodun özel durumu yakalamak, yığına yukarı yayar Transact-SQL kod ve çıktıda kere olur.Çağıran kodun özel durumu yakalamak, kanalın tüketici hala hata görür, ancak yinelenen bir hata değil.

Yalnızca alabilir bir SqlCommand ; içerik bağlantısı ile ilişkili Bu bağlam dışı bağlantı ile ilişkilendirilmiş bir komutu alamıyor.

Özel sonuç kümeleri döndüren

Yönetilen saklı yordamlar gelir değil sonuç kümeleri gönderebilirsiniz bir SqlDataReader.The SendResultsStart method, along with SendResultsRow and SendResultsEnd, allows stored procedures to send custom result sets to the client.

SendResultsStartgeçen bir SqlDataRecord olarak bir giriş.Başlangıcını işaretleyen bir sonuç küme ve kaydın meta veriler tanımlayan meta veriler oluşturmak için kullandığı sonuç küme.Kaydı ile değerini göndermek SendResultsStart.Kullanılarak gönderilen tüm sonraki satırlar, SendResultsRow, birbirlerine uymalı o meta veriler tanımı.

Not

Sonra arama SendResultsStart yöntem yalnızca SendResultsRow ve SendResultsEnd çağrılabilir.Calling any other method in the same instance of SqlPipe causes an InvalidOperationException.SendResultsEnd sets SqlPipe back to the initial state in which other methods can be called.

Örnek

The uspGetProductLine stored procedure returns the name, product number, color, and list price of all products within a specified product line.Bu saklı yordam için tam eşleşmelerin kabul 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

Aşağıdaki Transact-SQLdeyim yürütür uspGetProduct yordamı, yarış listesini döndürürbisikleti ürünleri.

EXEC uspGetProductLineVB 'T'