SqlCacheDependency.SqlCacheDependency(String, String) Constructor

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
)
public SqlCacheDependency (
	String databaseEntryName, 
	String tableName
)
public function SqlCacheDependency (
	databaseEntryName : String, 
	tableName : String
)
Not applicable.

Parameters

databaseEntryName

The name of a database defined in the databases Element for sqlCacheDependency for caching (ASP.NET Settings Schema) element of the application's Web.config file.

tableName

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

Exception typeCondition

HttpException

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.

ArgumentException

The tableName parameter is String.Empty.

ConfigurationErrorsException

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.

DatabaseNotEnabledForNotificationException

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

TableNotEnabledForNotificationException

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

ArgumentNullException

databaseEntryName is a null reference (Nothing in Visual Basic).

- or -

tableName is a null reference (Nothing in Visual Basic).

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.

<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" 
            connectionStringName="pubs"
            pollTime="9000000"
            />
        </databases>
      </sqlCacheDependency>
    </caching>
  </system.web>
</configuration>

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 Aspnet_regsql.exe command-line tool to set up the database for notifications. If a TableNotEnabledForNotificationException is thrown, you can call the SqlCacheDependencyAdmin.EnableTableForNotifications method or use Aspnet_regsql.exe 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.

Sub Page_Load(Src As Object, E As EventArgs)
   ' Declare the SqlCacheDependency instance, SqlDep.
   Dim SqlDep As SqlCacheDependency

   ' 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") Is Nothing

      ' 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 exDBDis As DatabaseNotEnabledForNotificationException
         Try
            SqlCacheDependencyAdmin.EnableNotifications("Northwind")

         ' If the database does not have permissions set for creating tables,
         ' the UnauthorizedAccessException is thrown. Handle it by redirecting
         ' to an error page.
         Catch exPerm As UnauthorizedAccessException
             Response.Redirect(".\ErrorPage.htm")
         End Try

      ' Handle the TableNotEnabledForNotificationException with
            ' a call to the SqlCacheDependencyAdmin.EnableTableForNotifications method.
      Catch exTabDis As TableNotEnabledForNotificationException
         Try
            SqlCacheDependencyAdmin.EnableTableForNotifications( _
             "Northwind", "Categories")

         ' If a SqlException is thrown, redirect to an error page.
         Catch exc As SqlException
             Response.Redirect(".\ErrorPage.htm")
         End Try

      ' 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."

      End Try

    Else
       CacheMsg.Text = "The data was retrieved from the Cache."
    End If
End Sub

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show: