SqlCacheDependencyAdmin 类

定义

在使用基于轮询的依赖项时,执行 SQL Server 数据库上需要的管理任务以支持 SqlCacheDependency 类。 此类不能被继承。

C#
public static class SqlCacheDependencyAdmin
继承
SqlCacheDependencyAdmin

示例

下面的代码示例是一个简单的 ASP.NET 页,用于在 SQL Server 数据库表上启用或禁用更改通知。 它使用 SqlCacheDependencyAdmin 对象来管理在名为 的 MyConnectionString连接字符串中指定的数据库中的更改通知。 该示例包括两个文件,一个定义用户界面的.aspx文件和一个代码隐藏文件,其中包含 ASP.NET 事件的源代码。

第一个示例是定义用户界面的 .aspx 文件。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

ASP.NET (C#)

<%@ 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>

第二个示例是包含页面事件的源代码的代码隐藏文件。

C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Web.Caching;

public partial class cacheDependencyAdmincs : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    // Put page in 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.Visible = false;
  }

   protected void Page_PreRender(object sender, EventArgs e)
  {
    try
    {
      string[] enabledTablesList =
      SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(
        ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
      if (enabledTablesList.Length > 0)
      {
        enabledTables.DataSource = enabledTablesList;
        enabledTables.DataBind();
      }
      else
      {
        enabledTablesMsg.Text = "No tables are enabled for change notifications.";
        enabledTables.Visible = false;
        disableTable.Visible = false;
      }
    }
    catch (DatabaseNotEnabledForNotificationException ex)
    {
      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";
    }
  }
  protected void enableNotification_Click(object sender, EventArgs e)
  {
    SqlCacheDependencyAdmin.EnableNotifications(
      ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
  }
  protected void disableNotification_Click(object sender, EventArgs e)
  {
    SqlCacheDependencyAdmin.DisableNotifications(
      ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
  }
  protected void disableTable_Click(object sender, EventArgs e)
  {
    foreach (ListItem item in enabledTables.Items)
    {
      if (item.Selected)
      {
        SqlCacheDependencyAdmin.DisableTableForNotifications(
          ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString,
          item.Text);
      }
    }
  }
  protected void enableTable_Click(object sender, EventArgs e)
  {
    try
    {
      if (tableName.Text.Contains(";"))
      {
        string[] tables = tableName.Text.Split(new Char[] { ';' });
        for (int i = 0; i < tables.Length; i++)
          tables[i] = tables[i].Trim();

        SqlCacheDependencyAdmin.EnableTableForNotifications(
          ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString,
          tables);
      }
      else
      {
        SqlCacheDependencyAdmin.EnableTableForNotifications(
          ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString,
          tableName.Text);
      }
    }
    catch (HttpException ex)
    {
      enableTableErrorMsg.Text = "<br />" +
        "An error occurred enabling a table.<br />" +
        "The error message was: " +
        ex.Message;
      enableTableErrorMsg.Visible = true;
    }
  }
}

注解

可以使用此类的方法以编程方式为对象更改通知启用和禁用 SQL Server 数据库及其表 SqlCacheDependency 。 此外,此类还获取有关使用 GetTablesEnabledForNotifications 方法为更改通知启用数据库中的表的信息。 但是,也可以使用 Aspnet_regsql 命令行工具来管理 SQL Server 数据库及其表的更改通知。

备注

若要调用此类中的任何方法,用于访问 SQL Server 数据库的帐户必须具有创建表和存储过程的权限。 若要对特定表启用通知,必须有权在该表上创建 SQL Server 触发器。 有关如何设置数据库权限的详细信息,请参阅 SQL Server 文档。 有关 ASP.NET 进程使用的帐户的详细信息,请参阅 ASP.NET 模拟

方法

DisableNotifications(String)

禁用指定数据库的 SqlCacheDependency 更改通知。

DisableTableForNotifications(String, String)

禁用 SQL Server 数据库表的 SqlCacheDependency 更改通知。

DisableTableForNotifications(String, String[])

禁用 SQL Server 数据库表的数组的 SqlCacheDependency 更改通知。

EnableNotifications(String)

启用指定数据库的 SqlCacheDependency 更改通知。

EnableTableForNotifications(String, String)

连接到指定 SQL Server 数据库并为 SqlCacheDependency 更改通知启用指定数据库表。

EnableTableForNotifications(String, String[])

连接到指定 SQL Server 数据库并为 SqlCacheDependency 更改通知启用数据库表的指定数组。

GetTablesEnabledForNotifications(String)

检索一个字符串数组,该数组包含 SQL Server 数据库中为更改通知启用的每个表的名称。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另请参阅