This documentation is archived and is not being maintained.

SqlCacheDependencyAdmin Class

Performs administrative tasks required on a SQL Server database to support the SqlCacheDependency class when using polling-based dependencies. This class cannot be inherited.

System.Object
  System.Web.Caching.SqlCacheDependencyAdmin

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

'Declaration
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.High)> _
Public NotInheritable Class SqlCacheDependencyAdmin

The SqlCacheDependencyAdmin type exposes the following members.

  NameDescription
Public methodStatic memberDisableNotificationsDisables SqlCacheDependency change notifications for the specified database.
Public methodStatic memberDisableTableForNotifications(String, String)Disables SqlCacheDependency change notifications on a SQL Server database table.
Public methodStatic memberDisableTableForNotifications(String, String())Disables SqlCacheDependency change notifications on an array of SQL Server database tables.
Public methodStatic memberEnableNotificationsEnables SqlCacheDependency change notifications on the specified database.
Public methodStatic memberEnableTableForNotifications(String, String)Connects to the specified SQL Server database and enables the specified database table for SqlCacheDependency change notifications.
Public methodStatic memberEnableTableForNotifications(String, String())Connects to the specified SQL Server database and enables the specified array of database tables for SqlCacheDependency change notification.
Public methodStatic memberGetTablesEnabledForNotificationsRetrieves a string array containing the name of every table that is enabled for change notifications in a SQL Server database.
Top

You can use the methods of this class to programmatically enable and disable a SQL Server database and its tables for SqlCacheDependency object change notifications. In addition, this class obtains information about which tables in a database are enabled for change notifications using the GetTablesEnabledForNotifications method. However, you can also use the Aspnet_regsql command-line tool to manage change notifications for a SQL Server database and its tables.

NoteNote

To call any of the methods in this class, the account you use to access the SQL Server database must have permissions to create tables and stored procedures. To enable notifications on a specific table, you must have permissions to create a SQL Server trigger on that table. For more information on how to set permissions on a database, see the SQL Server documentation. For more information about the account used by the ASP.NET process, see ASP.NET Impersonation.

The following code example is a simple ASP.NET page for enabling and disabling change notifications on SQL Server database tables. It uses the SqlCacheDependencyAdmin object to manage change notifications in the database specified in a connection string called MyConnectionString. The example includes two files, an .aspx file that defines the user interface and a code-behind file containing the source code for the ASP.NET events.

The first sample is the .aspx file that defines the user interface.

Security noteSecurity Note

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cacheDependencyAdmincs.aspx.cs"
  Inherits="cacheDependencyAdmincs" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <meta http-equiv="Content-Type" content="text/html" />
  <title>Cache Dependency Administration</title>
</head>
<body>
  <form id="form1" runat="server">
    <table>
      <tr>
        <td colspan="2">
          Database support for change notifications:
        </td>
      </tr>
      <tr>
        <td align="center">
          <asp:Button ID="enableNotification" runat="server" Text="On" OnClick="enableNotification_Click" />
        </td>
        <td align="center">
          <asp:Button ID="disableNotification" runat="server" Text="Off" OnClick="disableNotification_Click" />
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <asp:Label ID="enabledTablesMsg" runat="server" Text="Tables enabled for change notification:" />
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <asp:ListBox ID="enabledTables" runat="server" SelectionMode="multiple" /><br />
          <asp:Button ID="disableTable" runat="server" Text="Disable selected table(s)" OnClick="disableTable_Click" />
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <asp:Label ID="tableEnableMsg" runat="server" Text="Enable change notification on table:" />
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <asp:TextBox ID="tableName" runat="server" /><br />
          <asp:Button ID="enableTable" runat="server" Text="Enable table(s)" OnClick="enableTable_Click" />
          <asp:Label id="enableTableErrorMsg" runat="server" Visible="false" />
        </td>
      </tr>
    </table>
  </form>
</body>
</html>


The second sample is the code-behind file containing the source code for the page events.


Partial Class cacheDependencyAdminvb
  Inherits System.Web.UI.Page

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    'Put the page into a default state.
    enabledTables.Visible = True
    disableTable.Visible = True
    enabledTablesMsg.Text = "Tables enabled for change notification:"

    tableName.Visible = True
    enableTable.Visible = True
    tableEnableMsg.Text = "Enable change notification on table(s):"
    enableTableErrorMsg.Text = ""
  End Sub

  Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
    Try
      Dim enabledTablesList As String()
      enabledTablesList = SqlCacheDependencyAdmin.GetTablesEnabledForNotifications( _
        ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString)
      If enabledTablesList.Length > 0 Then
        enabledTables.DataSource = enabledTablesList
        enabledTables.DataBind()
      Else
        enabledTablesMsg.Text = "No tables are enabled for change notifications."
        enabledTables.Visible = False
        disableTable.Visible = False
      End If
    Catch ex As DatabaseNotEnabledForNotificationException
      enabledTables.Visible = False
      disableTable.Visible = False
      enabledTablesMsg.Text = "Cache notifications are not enabled in this database."

      tableName.Visible = False
      enableTable.Visible = False
      tableEnableMsg.Text = "Must enable database for notifications before enabling tables."
    End Try
  End Sub
  Protected Sub enableNotification_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    SqlCacheDependencyAdmin.EnableNotifications( _
        ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString)
  End Sub
  Protected Sub disableNotification_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    SqlCacheDependencyAdmin.DisableNotifications( _
        ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString)
  End Sub
  Protected Sub disableTable_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    For Each item As ListItem In enabledTables.Items
      If item.Selected Then
        SqlCacheDependencyAdmin.DisableTableForNotifications( _
        ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString, _
        item.Text)
      End If
    Next
  End Sub
  Protected Sub enableTable_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Try
      If tableName.Text.Contains(";") Then
        Dim tables As String()
        tables = tableName.Text.Split(New [Char]() {";"c})
        For i As Integer = 0 To tables.Length - 1
          tables(i) = tables(i).Trim
        Next

        SqlCacheDependencyAdmin.EnableTableForNotifications( _
          ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString, _
          tables)
      Else
        SqlCacheDependencyAdmin.EnableTableForNotifications( _
          ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString, _
          tableName.Text)
      End If
    Catch ex As HttpException
      enableTableErrorMsg.Text = "<br />" & _
        "An error occured enabling a table.<br />" & _
        "The error message was: " & _
        ex.Message
      enableTableErrorMsg.Visible = True
    End Try
  End Sub

End Class


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show: