SqlCacheDependencyAdmin 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在 SQL Server 資料庫上執行所需的管理工作,以在使用輪詢式相依性時支援 SqlCacheDependency 類別。 此類別無法獲得繼承。
public ref class SqlCacheDependencyAdmin abstract sealed
public static class SqlCacheDependencyAdmin
type SqlCacheDependencyAdmin = class
Public Class SqlCacheDependencyAdmin
- 繼承
-
SqlCacheDependencyAdmin
範例
下列程式代碼範例是一個簡單的 ASP.NET 頁面,可在 SQL Server 資料庫數據表上啟用和停用變更通知。 它會使用 SqlCacheDependencyAdmin 物件來管理名為的連接字串 MyConnectionString
中指定的資料庫中的變更通知。 此範例包含兩個檔案:定義使用者介面的.aspx檔案,以及包含 ASP.NET 事件的原始程式碼後置檔案。
第一個範例是定義使用者介面的.aspx檔案。
重要
這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 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>
<%@ Page Language="VB" CodeFile="cacheDependencyAdminvb.aspx.vb"
Inherits="cacheDependencyAdminvb"%>
<!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>
第二個範例是程式代碼後置檔案,其中包含頁面事件的原始程式碼。
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;
}
}
}
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 occurred enabling a table.<br />" & _
"The error message was: " & _
ex.Message
enableTableErrorMsg.Visible = True
End Try
End Sub
End Class
備註
您可以使用這個類別的方法,以程式設計方式啟用和停用 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 資料庫中每個已啟用變更告知的資料表名稱。 |