Aracılığıyla paylaş


Sorgu bildirimleri abone olmak için SQLNotificationRequest kullanma

Bir sorgu bildirim kullanmak için abone SqlNotificationRequest uygulama bildirimi isteyebilir önce alttaki Hizmet Aracısı nesneleri hazırlamanız gerekir.Abonelik isteği bir kez uygulamanız sıra için bir bildirim iletisi izler ve ileti geldiğinde uygun biçimde tepki verir.

SQL Server Hizmet Aracısı kullanarak bildirim sunar sorgu.Sorgu bildirim iletisi ileti türü adı olan https://schemas.microsoft.com/SQL/Notifications/QueryNotification.Hizmet Aracısı doğrulama iletileri bu tür VALID_XML şema KOLEKSİYONU ile.İle oluşturulmuş abonelikleri için SqlNotificationRequest, sıra izleme ve bildirim iletileri için uygulama sorumludur.Bu nedenle, kullanarak SqlNotificationRequest bir dış uygulama uygulamak gerekir.Bu konuda bir query bildirim'ı kullanmak için abone olmak için gereken belirli adımlar anlatılmaktadır SqlNotificationRequest.Sorgu bildirim iletileri işlemek için bir uygulama oluşturma hakkında daha fazla bilgi için bkz: Programlama ile Hizmet Aracısı yararları.

A SqlNotificationRequest bildirim iletileri almak üzere bir hizmet belirtmeniz gerekir.Bir hizmet oluşturmak için kullanılacak hizmeti için bir sıra oluşturmak ve sonra hizmet oluşturmanız gerekir.Ayrıca, yerel veritabanında hizmet için bir yol oluşturmanız gerekir.

The Veritabanı Altyapısı uses the contract https://schemas.microsoft.com/SQL/Notifications/PostQueryNotification to send notification messages, so the service that you create must accept conversations that follow this contract.Aşağıdaki örnek adlı bir hizmet oluşturur WebCacheNotifications sıra kullanan WebCacheMessages sonra bir yol oluşturur ve WebCacheNotifications yerel veritabanındaki hizmet.

USE AdventureWorks2008R2 ;

CREATE QUEUE WebSiteCacheMessages ;

CREATE SERVICE WebCacheNotifications
  ON QUEUE WebSiteCacheMessages
  ([https://schemas.microsoft.com/SQL/Notifications/PostQueryNotification]) ;

CREATE ROUTE
  WebCacheMessagesRoute
  WITH SERVICE_NAME = 'WebCacheNotifications',
       ADDRESS = 'LOCAL' ;

anlaşma https://schemas.microsoft.com/SQL/Notifications/PostQueryNotification belirtir bu türden iletileri https://schemas.microsoft.com/SQL/Notifications/QueryNotification Başlatıcı görüşme tarafından gönderilebilir

Hizmet adı, SqlNotificationRequest nesnedir Hizmet Aracısı hizmetin adı.Bildirim ileti Hizmet Aracısı olarak oluşturulur.

Bildirim isteği de isteği için bir ileti dize içermelidir.Zaman Veritabanı Altyapısı bir bildirim oluşturur bu istek için bildirim iletisi bu ileti dize içeriyor.Bir xml belgesi bildirim iletisidir.Bu belgeyi içeren bir ileti bildirim istekte ileti dize içeren öğe.Uygulama ileti dize bildirim için karşılık gelen bir sorgu tanımlamak için kullanır.

Bildirimi abonelikleri, sorgu ve ileti birleşimi kullanılarak yönetilir.Uygulama ile aynı iletiyi ve aynı sorguyu başka bir bildirim isterse Veritabanı Altyapısı güncelleştirmeleri bildirim aboneliği çok oluşturma yeni bir abonelik.İletiyi herhangi bir dize olabilir.Ancak, dikkat Veritabanı Altyapısı iki ileti aynı olup olmadığını belirler.Bu nedenle, Seçenekler küme karşılaştırmak için veritabanı dizeleri programınızda eşdeğer veritabanında eşdeğer olabilir.Örneğin, Veritabanı Altyapısı yalnızca sayısını izleyen boşluklar aynı olacak şekilde farklı dizeleri nitelendirir.

Aşağıdaki örnek kullanarak bir bildirim abonelik oluşturan basit bir program gösterir SqlNotificationRequest:

[Visual Basic]

Option Strict On

Imports System
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient


Namespace Microsoft.Samples.SqlServer

Module NotificationSampleMain


    Public Sub Main()

        Try

            ' Connect to the AdventureWorks2008R2 database in the default instance
            ' on this server, using integrated security.  If you change this
            ' connection string, be sure to change the service string below.

            Using connection As SqlConnection = _
                new SqlConnection("database=AdventureWorks2008R2;server=.;" + _
                                  "Integrated Security=SSPI")

                connection.Open()

                ' Define the service to receive the notifications. Update this
                ' information if you change the connection string.

                Dim service As String = _
                    "WebCacheNotifications"

                Dim query As String = _
                        "SELECT prod.Name, prod.Class, " + _
                        "       prod.ProductNumber " + _
                        "FROM Production.Product as prod " + _
                        "WHERE prod.Color = 'Black' " 

                Dim command As SqlCommand = connection.CreateCommand()

                command.CommandText = query

                command.Notification = _
                    new SqlNotificationRequest(Guid.NewGuid().ToString(), _
                                               service, _
                                               Int32.MaxValue)

               Dim reader As SqlDataReader = command.ExecuteReader()

               ' Normally, an application would process the results here.

               MsgBox("Registered the notification.")
                  
            ' Notice that the connection dispose method also
            ' disposes the commands and readers created from the
            ' connection.

            End Using  ' Using connection

            

        ' For sample purposes, simply display all exceptions and exit.

        Catch e As SqlException
               MsgBox("SqlException: " + e.Message + vbCrLf  _
                                       + e.StackTrace )
        Catch e As Exception
               MsgBox("Exception: " + e.Message + vbCrLf  _
                                       + e.StackTrace )
        End Try

    End Sub ' Main

End Module 'NotificationSampleMain

End Namespace ' Microsoft.Samples.SqlServer

Bu kod çalıştıktan sonra SQL Server içeren bir sorgu bildirim abonelik.abonelik Aşağıdaki sorguda belirtilen veri herhangi bir değişiklik olduğunda bir uyarı üretir:

SELECT prod.Name, prod.Class, prod.ProductNumber
FROM Products.Product as prod
    WHERE prod.Color = 'Black'

Hizmet Aracısı hizmet için bildirim iletileri teslim WebCacheNotifications.Bu hizmet sıra kullandığından WebCacheMessages, o sırada bildirim iletileri görüntülenir.Bildirim iletileri işlemek için uygulama sırası izler WebCacheMessages.