Export (0) Print
Expand All

SqlDependency Class

The SqlDependency object represents a query notification dependency between an application and an instance of SQL Server. An application can create a SqlDependency object and register to receive notifications via the OnChangeEventHandler event handler.

System.Object
  System.Data.SqlClient.SqlDependency

Namespace:  System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)

public sealed class SqlDependency

The SqlDependency type exposes the following members.

  NameDescription
Public methodSqlDependency()Creates a new instance of the SqlDependency class with the default settings.
Public methodSqlDependency(SqlCommand)Creates a new instance of the SqlDependency class and associates it with the SqlCommand parameter.
Public methodSqlDependency(SqlCommand, String, Int32)Creates a new instance of the SqlDependency class, associates it with the SqlCommand parameter, and specifies notification options and a time-out value.
Top

  NameDescription
Public propertyHasChangesGets a value that indicates whether one of the result sets associated with the dependency has changed.
Public propertyIdGets a value that uniquely identifies this instance of the SqlDependency class.
Top

  NameDescription
Public methodAddCommandDependencyAssociates a SqlCommand object with this SqlDependency instance.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodStatic memberStart(String)Starts the listener for receiving dependency change notifications from the instance of SQL Server specified by the connection string.
Public methodStatic memberStart(String, String)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.
Public methodStatic memberStop(String)Stops a listener for a connection specified in a previous Start call.
Public methodStatic memberStop(String, String)Stops a listener for a connection specified in a previous Start call.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public eventOnChangeOccurs when a notification is received for any of the commands associated with this SqlDependency object.
Top

SqlDependency is ideal for caching scenarios, where your ASP.NET application or middle-tier service needs to keep certain information cached in memory. SqlDependency allows you to receive notifications when the original data in the database changes so that the cache can be refreshed.

To set up a dependency, you need to associate a SqlDependency object to one or more SqlCommand objects. To receive notifications, you need to subscribe to the OnChange event. For more information about the requirements for creating queries for notifications, see "Creating a Query for Notification" in SQL Server Books Online.

NoteNote

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. 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 topic in SQL Server Books Online.

For more information, see Query Notifications in SQL Server and "Building Notification Solutions" in SQL Server Books Online.

NoteNote

The OnChange event may be generated on a different thread from the thread that initiated command execution.

Query notifications are supported only for SELECT statements that meet a list of specific requirements. The following table provides links to the Service Broker and Query Notifications documentation in SQL Server Books Online.

SQL Server Books Online

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft