语言
要检测查询结果何时与最初检索到的结果不同,请在执行命令之前将 SqlDependency 对象与 SqlCommand 关联。 订阅 OnChange 事件,以便在结果更改时收到通知。 还可以检查依赖项的 HasChanges 属性,以确定结果是否已更改。
安全注意事项
若要在基础数据更改时接收通知,请调用 Start。 依赖项基础结构会为侦听器打开 SqlConnection。 向数据库主体授予所需的 Service Broker 和查询通知权限。 新式.NET不支持代码访问安全性。 有关详细信息,请参阅“启用查询通知”。
示例
以下步骤演示了如何声明依赖项、执行命令以及在结果集更改时接收通知:
在应用程序初始化时,调用
SqlDependency.Start以启动客户端侦听器。创建 SqlConnection 和 SqlCommand 对象以连接到服务器并定义 Transact-SQL (T-SQL) 语句。
创建或重复使用该
SqlDependency对象并将其与SqlCommand对象相关联。 依赖项会根据需要创建 SqlNotificationRequest 对象并将其与命令相关联。 通知请求包含依赖项的内部标识符。 构造函数不启动侦听器。向
OnChange对象的SqlDependency事件订阅一个事件处理程序。使用其某一种
Execute方法执行命令。 由于该命令包含通知请求,服务器会将该查询注册到指定队列以接收通知。在应用程序终止时,使用相同的连接字符串和队列名称调用
SqlDependency.Stop。
如果任何用户随后更改了底层数据,Microsoft SQL Server 将检测到有此类更改的通知正在等待,并发布该通知,然后通过调用 SqlConnection 创建的底层 SqlDependency.Start 进行处理并转发给客户端。 客户端侦听器接收失效消息。 然后,客户端侦听器查找关联的 SqlDependency 对象,并激发 OnChange 事件。
以下代码片段显示了示例应用程序的设计模式。 安装 Microsoft.Data.SqlClient NuGet 包,并在 C# 或 Visual Basic 中导入 Microsoft.Data.SqlClient。
这些片段使用现有的 Service Broker 队列。 将 设置为队列的未限定名称,并将 设置为 。 指定将通知路由到该队列的服务。 将相同的连接字符串和队列名称传递给Stop和Start。 有关详细信息,请参阅“启用查询通知”。
Sub Initialization()
' Create a dependency connection.
SqlDependency.Start(connectionString, queueName)
End Sub
Sub SomeMethod()
' Assume connection is an open SqlConnection.
' Create a new SqlCommand object.
Using command As New SqlCommand( _
"SELECT ShipperID, CompanyName, Phone FROM dbo.Shippers", _
connection)
' Create a dependency and associate it with the SqlCommand.
Dim dependency As New SqlDependency(command, notificationOptions, 0)
' Maintain the reference in a class member.
' Subscribe to the SqlDependency event.
AddHandler dependency.OnChange, AddressOf OnDependencyChange
' Execute the command.
Using reader = command.ExecuteReader()
' Process the DataReader.
End Using
End Using
End Sub
' Handler method
Sub OnDependencyChange(ByVal sender As Object, _
ByVal e As SqlNotificationEventArgs)
' Handle the event (for example, invalidate this cache entry).
End Sub
Sub Termination()
' Release the dependency
SqlDependency.Stop(connectionString, queueName)
End Sub
void Initialization()
{
// Create a dependency connection.
SqlDependency.Start(connectionString, queueName);
}
void SomeMethod()
{
// Assume connection is an open SqlConnection.
// Create a new SqlCommand object.
using (SqlCommand command=new SqlCommand(
"SELECT ShipperID, CompanyName, Phone FROM dbo.Shippers",
connection))
{
// Create a dependency and associate it with the SqlCommand.
SqlDependency dependency=new SqlDependency(command, notificationOptions, 0);
// Maintain the reference in a class member.
// Subscribe to the SqlDependency event.
dependency.OnChange+=new
OnChangeEventHandler(OnDependencyChange);
// Execute the command.
using (SqlDataReader reader = command.ExecuteReader())
{
// Process the DataReader.
}
}
}
// Handler method
void OnDependencyChange(object sender,
SqlNotificationEventArgs e )
{
// Handle the event (for example, invalidate this cache entry).
}
void Termination()
{
// Release the dependency.
SqlDependency.Stop(connectionString, queueName);
}