הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Built upon the Service Broker infrastructure, query notifications allow applications to be notified when data has changed. This feature is particularly useful for applications that provide a cache of information from a database, such as a Web application, and need to be notified when the source data is changed.
Microsoft.Data.SqlClient provides two classes for query notifications:
Use SqlNotificationRequest to execute a command with a notification request and process the Service Broker messages yourself.
Use SqlDependency to detect changes through the
OnChangeevent. Microsoft.Data.SqlClient manages the listener and exposes notification information in the event arguments.
Important
Don't use System.Data.SqlClient for new development. Use Microsoft.Data.SqlClient instead. The ASP.NET Framework SqlCacheDependency class requires System.Data.SqlClient and doesn't integrate with Microsoft.Data.SqlClient. For new Microsoft.Data.SqlClient applications, subscribe to SqlDependency.OnChange and invalidate entries through your application's cache API.
Query notifications are used for applications that need to refresh displays or caches in response to changes in underlying data. SQL Server allows .NET applications to send a command and request notification if the same command would produce result sets different from those initially retrieved. Notifications generated at the server are sent through queues to be processed later.
You can set up notifications for SELECT and EXECUTE statements. When using an EXECUTE statement, SQL Server registers a notification for the command executed rather than the EXECUTE statement itself. The command must meet the requirements and limitations for a SELECT statement. When a command that registers a notification contains more than one statement, the Database Engine creates a notification for each statement in the batch.
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. For more information about Query Notifications and SQL Server Service Broker, see the following links to articles in the SQL Server documentation.
SQL Server documentation
In This Section
Enabling Query Notifications Discusses how to use query notifications, including the requirements for enabling and using them.
Detecting Changes with SqlDependency Demonstrates how to detect when query results will be different from those originally received.
SqlCommand Execution with a SqlNotificationRequest Demonstrates how to configure the SqlCommand object for a query notification.
Reference
SqlNotificationRequest Describes the SqlNotificationRequest class and all of its members.
SqlDependency Describes the SqlDependency class and all of its members.
SqlCacheDependency Describes the ASP.NET Framework cache dependency class for System.Data.SqlClient. It doesn't wrap Microsoft.Data.SqlClient dependencies.