SqlCacheDependency Constructor (String, String)

SqlCacheDependency Constructor (String, String)


Initializes a new instance of the SqlCacheDependency class, using the supplied parameters to create a cache-key dependency.

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

public SqlCacheDependency(
	string databaseEntryName,
	string tableName


Type: System.String

The name of a database defined in the databases element of the application's Web.config file.

Type: System.String

The name of the database table that the SqlCacheDependency is associated with.

Exception Condition

The internal check for SqlClientPermission failed.

- or -

The databaseEntryName was not found in the list of databases configured for table-based notifications.

- or -

The SqlCacheDependency object could not connect to the database during initialization.

- or -

The SqlCacheDependency object encountered a permission-denied error either on the database or on the database stored procedures that support the SqlCacheDependency object.


The tableName parameter is String.Empty.


Polling is not enabled for the SqlCacheDependency.

- or -

The polling interval is not correctly configured.

- or -

No connection string was specified in the application's configuration file.

- or -

The connection string specified in the application's configuration file could not be found.

- or -

The connection string specified in the application's configuration file is an empty string.


The database specified in the databaseEntryName parameter is not enabled for change notifications.


The database table specified in the tableName parameter is not enabled for change notifications.


databaseEntryName is null.

- or -

tableName is null.

This constructor is used to create SqlCacheDependency objects for SQL Server 7.0 and SQL Server 2000 products.

The database name passed to the database parameter must be defined in the application's Web.config file. For example, the following Web.config file defines a database named pubs for SqlCacheDependency change notifications.

    <add name="Pubs" connectionString="Data Source=(local); Initial Catalog=pubs; Integrated Security=true"; providerName="System.Data.SqlClient" />
      <sqlCacheDependency enabled = "true" pollTime = "60000" >
          <add name="pubs" 

Two exceptions are commonly thrown when this constructor is used: DatabaseNotEnabledForNotificationException and TableNotEnabledForNotificationException. If a DatabaseNotEnabledForNotificationException is thrown, you can call the SqlCacheDependencyAdmin.EnableNotifications method in exception-handling code, or use the command-line tool to set up the database for notifications. If a TableNotEnabledForNotificationException is thrown, you can call the SqlCacheDependencyAdmin.EnableTableForNotifications method or use to set up the table for notifications.

The following code example uses this constructor to create an instance of the SqlCacheDependency class that is associated with a database table named Categories in a SQL Server database named Northwind.

public void Page_Load(object Src, EventArgs E) 
    // Declare the SqlCacheDependency instance, SqlDep. 
    SqlCacheDependency SqlDep = null; 

    // Check the Cache for the SqlSource key. 
    // If it isn't there, create it with a dependency 
    // on a SQL Server table using the SqlCacheDependency class. 
    if (Cache["SqlSource"] == null) { 

        // Because of possible exceptions thrown when this 
        // code runs, use Try...Catch...Finally syntax. 
        try { 
            // Instantiate SqlDep using the SqlCacheDependency constructor. 
            SqlDep = new SqlCacheDependency("Northwind", "Categories"); 

        // Handle the DatabaseNotEnabledForNotificationException with 
        // a call to the SqlCacheDependencyAdmin.EnableNotifications method. 
        catch (DatabaseNotEnabledForNotificationException exDBDis) { 
            try { 

            // If the database does not have permissions set for creating tables, 
            // the UnauthorizedAccessException is thrown. Handle it by redirecting 
            // to an error page. 
            catch (UnauthorizedAccessException exPerm) { 

        // Handle the TableNotEnabledForNotificationException with 
        // a call to the SqlCacheDependencyAdmin.EnableTableForNotifications method. 
        catch (TableNotEnabledForNotificationException exTabDis) { 
            try { 
                SqlCacheDependencyAdmin.EnableTableForNotifications("Northwind", "Categories"); 

            // If a SqlException is thrown, redirect to an error page. 
            catch (SqlException exc) { 

        // If all the other code is successful, add MySource to the Cache 
        // with a dependency on SqlDep. If the Categories table changes, 
        // MySource will be removed from the Cache. Then generate a message 
        // that the data is newly created and added to the cache. 
        finally { 
            Cache.Insert("SqlSource", Source1, SqlDep); 
            CacheMsg.Text = "The data object was created explicitly."; 


    else { 
        CacheMsg.Text = "The data was retrieved from the Cache."; 

.NET Framework
Available since 2.0
Return to top
© 2016 Microsoft