SqlDependency 类


SqlDependency 对象表示应用程序和 SQL Server 实例间的查询通知依赖关系。The SqlDependency object represents a query notification dependency between an application and an instance of SQL Server. 应用程序可以创建一个 SqlDependency 对象并进行注册以通过 OnChangeEventHandler 事件处理程序接收通知。An application can create a SqlDependency object and register to receive notifications via the OnChangeEventHandler event handler.

public ref class SqlDependency sealed
public sealed class SqlDependency
type SqlDependency = class
Public NotInheritable Class SqlDependency


SqlDependency 非常适合用于缓存方案,ASP.NET 应用程序或中间层服务需要将某些信息缓存在内存中。SqlDependency is ideal for caching scenarios, where your ASP.NET application or middle-tier service needs to keep certain information cached in memory. SqlDependency 允许您在数据库中的原始数据发生更改时接收通知,以便刷新缓存。SqlDependency allows you to receive notifications when the original data in the database changes so that the cache can be refreshed.

若要设置依赖关系,需要将 SqlDependency 对象关联到一个或多个 SqlCommand 对象。To set up a dependency, you need to associate a SqlDependency object to one or more SqlCommand objects. 若要接收通知,需要订阅 OnChange 事件。To receive notifications, you need to subscribe to the OnChange event. 有关创建通知查询的要求的详细信息,请参阅使用查询通知For more information about the requirements for creating queries for notifications, see Working with Query Notifications.


SqlDependency 旨在在 ASP.NET 或中间层服务中使用,在该服务中,有相对较少的服务器依赖于数据库的依赖关系。SqlDependency was designed to be used in ASP.NET or middle-tier services where there is a relatively small number of servers having dependencies active against the database. 它不是用于在客户端应用程序中使用的,其中成百上千的客户端计算机将为单一数据库服务器设置 SqlDependency 对象。It was not designed for use in client applications, where hundreds or thousands of client computers would have SqlDependency objects set up for a single database server. 如果正在开发的应用程序在数据更改时需要可靠的第二秒通知,请查看规划通知一文中的 "规划有效的查询通知策略" 和 "查询通知的替代方案" 部分。If you are developing an application where you need reliable sub-second notifications when data changes, review the sections Planning an Efficient Query Notifications Strategy and Alternatives to Query Notifications in the Planning for Notifications article.

有关详细信息,请参阅SQL Server 中的查询通知生成通知解决方案For more information, see Query Notifications in SQL Server and Building Notification Solutions.


可能会在启动命令执行的线程的不同线程上生成 OnChange 事件。The OnChange event may be generated on a different thread from the thread that initiated command execution.

仅对满足一系列特定要求的 SELECT 语句支持查询通知。Query notifications are supported only for SELECT statements that meet a list of specific requirements.



用默认设置创建 SqlDependency 类的新实例。Creates a new instance of the SqlDependency class with the default settings.


创建 SqlDependency 类的新实例,并将其与 SqlCommand 参数关联。Creates a new instance of the SqlDependency class and associates it with the SqlCommand parameter.

SqlDependency(SqlCommand, String, Int32)

创建 SqlDependency 类的一个新实例,将其与 SqlCommand 参数关联,并指定通知选项和超时值。Creates a new instance of the SqlDependency class, associates it with the SqlCommand parameter, and specifies notification options and a time-out value.



获取一个值,该值指示与依赖项关联的结果集之一是否已经更改。Gets a value that indicates whether one of the result sets associated with the dependency has changed.


获取一个值,该值唯一标识 SqlDependency 类的此实例。Gets a value that uniquely identifies this instance of the SqlDependency class.



SqlCommand 对象与此 SqlDependency 实例关联。Associates a SqlCommand object with this SqlDependency instance.


确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

启动用于接收依赖项更改通知的侦听器,该通知来自由连接字符串指定的 SQL Server 实例。Starts the listener for receiving dependency change notifications from the instance of SQL Server specified by the connection string.

Start(String, String)

启动用于接收依赖项更改通知的侦听器,该侦听器使用指定的 SQL Server Service Broker 队列从由连接字符串指定的 SQL Server 实例接收。Starts the listener for receiving dependency change notifications from the instance of SQL Server specified by the connection string using the specified SQL Server Service Broker queue.


停止在上一次 Start 调用中指定的连接的侦听器。Stops a listener for a connection specified in a previous Start call.

Stop(String, String)

停止在上一次 Start 调用中指定的连接的侦听器。Stops a listener for a connection specified in a previous Start call.


返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)



当收到与此 SqlDependency 对象关联的任意命令的通知时发生。Occurs when a notification is received for any of the commands associated with this SqlDependency object.