On all supported versions of SQL Server (7.0, 2000, 2005) the SqlCacheDependency class monitors a specific SQL Server database table so that when the table changes, items associated with the table will be automatically removed from the Cache.
When the database table changes, the cached item is automatically deleted, and a new version of the item is added to the Cache.
The SqlCacheDependency class also supports integration with the System.Data.SqlClient.SqlDependency class when using a SQL Server 2005 database. The query notification mechanism of SQL Server 2005 is used to detect changes to data that invalidate the results of an SQL query. Any cached items associated with the SQL query are removed from the System.Web.Caching.Cache.
You can use the SqlCacheDependency class to add items to your application's Cache that are dependent on either a SQL Server database table or an SQL query when using SQL Server 2005. You can also use this class with the @ OutputCache directive to make an output-cached page or user control dependent on a SQL Server database table. Finally, you can use the SqlCacheDependency class with the @ OutputCache page directive to make an output-cached page dependent on the results of an SQL query when using SQL Server 2005. Query notification using SQL Server 2005 is not supported on the @ OutputCache directive for user controls.
Note |
|---|
| For this class to work correctly when using table-based notifications, the database and any tables that you want to make dependencies on must have notifications enabled. You can enable notifications by using the methods of the SqlCacheDependencyAdmin class or the Aspnet_regsql.exe command-line tool. Also, the proper configuration settings must be included in the application's Web.config file. Using a SqlCacheDependency object with SQL Server 2005 query notification does not require any explicit configuration. Developers should consult the SQL Server 2005 Books Online for restrictions on the types of Transact-SQL queries that are allowed when using query notification. |
The following is a sample ASP.NET Web.config file that enables table-based dependencies on a SQL Server database table.
|
<configuration>
<connectionStrings>
<add name="Pubs" connectionString="Data Source=(local); Initial Catalog=pubs; Integrated Security=true"; providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<caching>
<sqlCacheDependency enabled = "true" pollTime = "60000" >
<databases>
<add name="pubs"
connectionString = "pubs"
pollTime = "9000000"
/>
</databases>
</sqlCacheDependency>
</caching>
</system.web>
</configuration> |