使用 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 必须指定服务以接收通知消息。若要创建服务,必须先创建服务要使用的队列,然后再创建服务。还必须在本地数据库中创建到服务的路由。
数据库引擎 使用约定 https://schemas.microsoft.com/SQL/Notifications/PostQueryNotification
来发送通知消息,因此创建的服务必须接受遵循此约定的会话。下面的示例创建了使用队列 WebCacheMessages 的名为 WebCacheNotifications 的服务,然后在本地数据库中创建了到 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 消息。
通知请求还必须包含该请求的消息字符串。当数据库引擎 为此请求创建通知时,通知消息将包含此消息字符串。该通知消息是 XML 文档。此文档包含了 Message 元素,该元素容纳了通知请求中所包含的消息字符串。应用程序使用消息字符串来识别响应通知的查询。
使用查询和消息的组合来管理通知订阅。如果应用程序用请求具有相同消息和相同查询的其他通知,则数据库引擎 将更新通知订阅,而无需创建新的订阅。消息可以是任意字符串。但请注意,由数据库引擎 确定两条消息是否相同。因此,数据库字符串的选项集在程序中经比较认为是不等效的,但在数据库中可能是等效的。 例如,数据库引擎 会认为只有尾部空格个数不同的字符串是相同的。
下面的示例显示了一个简单的程序,该程序使用 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。
请参阅
概念
其他资源
Introduction to Service Broker Programming
Service Broker 路由