Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
SqlDependency Class

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
SqlDependency Class

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

Namespace:  System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)
Visual Basic (Declaration)
Public NotInheritable Class SqlDependency
Visual Basic (Usage)
Dim instance As SqlDependency
C#
public sealed class SqlDependency
Visual C++
public ref class SqlDependency sealed
JScript
public final class SqlDependency

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.

SqlDependency requires the .NET Framework version 2.0 and SQL Server 2005. For more information, see Query Notifications in SQL Server (ADO.NET) 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.

System..::.Object
  System.Data.SqlClient..::.SqlDependency
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Service Broker Permissions      KeithElder   |   Edit   |   Show History
You have to enable Service Broker to get SqlDependency to work. We spent a lot of time at work trying to get this to work in an Enterprise setup. Here's a blog entry about which permissions you need to use.

http://keithelder.net/blog/archive/2009/01/20/sqldependency-and-sql-service-broker-permissions.aspx


SQL Dependency Recursive CTE T-SQL      Jeff Fischer ... Thomas Lee   |   Edit   |   Show History

Refer to my blog article describing a SQL dependency solution using a recursive CTE.

http://blog.scoftware.com/post/2009/05/29/SQL-Server-Dependency-Chain-T-SQL.aspx?utm_campaign=blog&utm_source=msdn&utm_medium=organic

Jeff Fischer

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker