System.Web.Caching.SqlCacheDependency Class

Establishes a relationship between an item stored in an ASP.NET application's System.Web.Caching.Cache object and either a specific SQL Server database table or the results of a sqprsqlong query. This class cannot be inherited.

See Also: SqlCacheDependency Members

Syntax

public sealed class SqlCacheDependency : CacheDependency

Remarks

On all supported versions of SQL Server (Microsoft SQL Server 7.0, Microsoft SQL Server 2000, and SQL Server 2005) the System.Web.Caching.SqlCacheDependency class monitors a specific SQL Server database table. When the table changes, items associated with the table are removed from the System.Web.Caching.Cache, and a new version of the item is added to the System.Web.Caching.Cache.

The System.Web.Caching.SqlCacheDependency class also supports integration with the System.Data.SqlClient.SqlDependency class when using a sqprsqlong database. The query notification mechanism of sqprsqlong detects changes to data that invalidate the results of an SQL query and removes any cached items associated with the SQL query from the System.Web.Caching.Cache.

You can use the System.Web.Caching.SqlCacheDependency class to add items to your application's System.Web.Caching.Cache that are dependent on either a SQL Server database table or on an SQL query when using sqprsqlong. You can also use this class with the @ OutputCache directive to make an output-cached page or a user control dependent on a SQL Server database table. Finally, you can use the System.Web.Caching.SqlCacheDependency class with the @ OutputCache page directive to make an output-cached page dependent on the results of an SQL query when using sqprsqlong. Query notification using sqprsqlong 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 calling methods of the System.Web.Caching.SqlCacheDependencyAdmin class or by using the Aspnet_regsql.exe command-line tool. In addition, the proper configuration settings must be included in the application's Web.config file.

Using a System.Web.Caching.SqlCacheDependency object with sqprsqlong query notification does not require any explicit configuration. Consult the sqprsqlong Books Online for information about restrictions on the types of Transact-SQL queries that are allowed when using query notification.

The following example shows an ASP.NET Web.config file that enables table-based dependencies on a SQL Server database table.

Example

<configuration>
  <connectionStrings>
    <add name="Northwind" connectionString="Data Source=(local); Initial Catalog=northwind; Integrated Security=true"; providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <caching>
      <sqlCacheDependency enabled = "true" pollTime = "60000" >
        <databases>
          <add name="northwind" 
            connectionStringName="Northwind"
            pollTime="9000000"
            />
        </databases>
      </sqlCacheDependency>
    </caching>
  </system.web>
</configuration>

Requirements

Namespace: System.Web.Caching
Assembly: System.Web (in System.Web.dll)
Assembly Versions: 2.0.0.0