使用 SqlNotificationRequest 执行 SqlCommand

可以将 SqlCommand 配置为在数据从服务器中提取后发生更改时生成通知,如果再次执行查询,结果集将不同。 这对于以下情况非常有用:你希望在服务器上使用自定义通知队列,或者不希望维护活动对象的情况。

创建通知请求

可以使用 SqlNotificationRequest 对象,通过将通知请求绑定到 SqlCommand 对象来创建请求。 创建请求后,你将不再需要 SqlNotificationRequest 对象。 可以在队列中查询任何通知,并做出相应响应。 即使应用程序关闭后重新启动,也会出现通知。

在对关联通知执行命令时,对原始结果集所做的任何更改都会触发向在通知请求中配置的 SQL Server 队列发送消息。

如何轮询 SQL Server 队列和解释该消息是应用程序特定的。 应用程序负责轮询队列并根据消息的内容做出响应。

备注

SqlDependency 中使用 SQL Server 通知请求时,请创建自己的队列名称,而不是使用默认的服务名称。

SqlNotificationRequest 没有新的客户端安全元素。 这主要是一种服务器功能,并且服务器已创建用户必须拥有的用于请求通知的特殊权限。

示例

下面的代码段演示如何创建 SqlNotificationRequest 并将其与 SqlCommand 相关联。

' Assume connection is an open SqlConnection.
' Create a new SqlCommand object.
Dim command As New SqlCommand( _
  "SELECT ShipperID, CompanyName, Phone FROM dbo.Shippers", connection)

' Create a SqlNotificationRequest object.
Dim notificationRequest As New SqlNotificationRequest()
notificationRequest.id = "NotificationID"
notificationRequest.Service = "mySSBQueue"

' Associate the notification request with the command.
command.Notification = notificationRequest
' Execute the command.
command.ExecuteReader()
' Process the DataReader.
' You can use Transact-SQL syntax to periodically poll the
' SQL Server queue to see if you have a new message.
// Assume connection is an open SqlConnection.
// Create a new SqlCommand object.
SqlCommand command=new SqlCommand(
 "SELECT ShipperID, CompanyName, Phone FROM dbo.Shippers", connection);

// Create a SqlNotificationRequest object.
SqlNotificationRequest notificationRequest=new SqlNotificationRequest();
notificationRequest.id="NotificationID";
notificationRequest.Service="mySSBQueue";

// Associate the notification request with the command.
command.Notification=notificationRequest;
// Execute the command.
command.ExecuteReader();
// Process the DataReader.
// You can use Transact-SQL syntax to periodically poll the
// SQL Server queue to see if you have a new message.

请参阅