共用方式為


使用 SqlNotificationRequest 訂閱查詢通知

使用 SqlNotificationRequest 訂閱查詢通知必須準備基礎 Service Broker 物件,您的應用程式才可要求通知。一旦要求訂閱,您的應用程式會監視通知訊息的佇列,並在訊息到達時做出適當的反應。

SQL Server 會使用 Service Broker 傳遞查詢通知。查詢通知訊息具有訊息類型名稱 https://schemas.microsoft.com/SQL/Notifications/QueryNotification。Service Broker 會驗證此類型的訊息是否為 VALID_XML WITH SCHEMA COLLECTION。對於 SqlNotificationRequest 所建立的訂閱,應用程式負責監視佇列和處理通知訊息。因此,使用 SqlNotificationRequest 需要您實作外部應用程式。本主題討論使用 SqlNotificationRequest 訂閱查詢通知所需的特定步驟。如需建立應用程式以處理查詢通知訊息的詳細資訊,請參閱<Introduction to Service Broker Programming>。

SqlNotificationRequest 必須指定服務以接收通知訊息。若要建立服務,您必須建立要使用的服務佇列,然後建立服務。您也必須在本機資料庫中建立服務的路由。

Database Engine 使用合約 https://schemas.microsoft.com/SQL/Notifications/PostQueryNotification 來傳送通知訊息,所以您所建立的服務必須接受遵照此合約的交談。下列範例會建立名為 WebCacheNotifications 的服務,該服務使用 WebCacheMessages 佇列,接著會在本機資料庫中建立 WebCacheNotifications 服務的路由。

USE AdventureWorks ;

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' ;

合約 https://schemas.microsoft.com/SQL/Notifications/PostQueryNotification 指定了交談的起始端可以傳送的訊息類型https://schemas.microsoft.com/SQL/Notifications/QueryNotification

SqlNotificationRequest 物件中的服務名稱是 Service Broker 服務的名稱。將會以 Service Broker 訊息建立通知。

通知要求必須也包含要求的訊息字串。當 Database Engine 建立此要求的通知時,通知訊息將包含此訊息字串。通知訊息是 XML 文件。此文件包含 Message 元素,它具有包括在通知要求中的訊息字串。應用程式使用訊息字串來識別與通知對應的查詢。

通知訂閱是使用查詢與訊息的組合來管理。如果應用程式要求含有相同訊息與相同查詢的另一個通知,Database Engine 會更新通知訂閱,而不會建立新訂閱。該訊息可為任何字串。不過,請注意 Database Engine 會判斷這兩個訊息是否相同。因此,為程式中不相等的資料庫字串所設定的選項,在資料庫中可能是相等的。例如,Database Engine 將僅尾端空白數目相異的字串視為相同。

下列範例顯示以 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 AdventureWorks 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=AdventureWorks;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

在執行此程式碼後,SQL Server 包含查詢通知訂閱。當下列查詢中所指定的資料有任何變更時,訂閱會產生通知:

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

Service Broker 會傳遞通知訊息給 WebCacheNotifications 服務。因為此服務使用 WebCacheMessages 佇列,所以通知訊息會出現在該佇列中。為了處理通知訊息,應用程式會監視 WebCacheMessages 佇列。

請參閱

概念

取得通知
使用 SqlDependency 訂閱查詢通知

其他資源

Introduction to Service Broker Programming
Service Broker 路由

說明及資訊

取得 SQL Server 2005 協助