SqlCacheDependency クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
ASP.NET アプリケーションの Cache オブジェクトに格納されている項目と、特定の SQL Server データベース テーブルまたは SQL Server 2005 クエリの結果との間にリレーションシップを確立します。 このクラスは継承できません。
public ref class SqlCacheDependency sealed : System::Web::Caching::CacheDependency
public sealed class SqlCacheDependency : System.Web.Caching.CacheDependency
type SqlCacheDependency = class
inherit CacheDependency
Public NotInheritable Class SqlCacheDependency
Inherits CacheDependency
- 継承
例
次のコード例では、 SqlDataSource コントロールと GridView コントロールを使用してデータベース テーブルを表示します。 ページが読み込まれると、ページは SqlCacheDependency オブジェクトの作成を試みます。
SqlCacheDependency オブジェクトが作成されると、ページは、SqlCacheDependency オブジェクトに依存する項目をCacheに追加します。 次に示すような例外処理を使用する必要があります。
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
// <snippet2>
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 {
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 (UnauthorizedAccessException exPerm) {
Response.Redirect(".\\ErrorPage.htm");
}
}
// 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) {
Response.Redirect(".\\ErrorPage.htm");
}
}
// 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.";
}
}
// </snippet2>
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<p>
</p>
<p>
<asp:SqlDataSource id="Source1" runat="server" SelectCommand="SELECT * FROM [Categories]" UpdateCommand="UPDATE [Categories] SET [CategoryName]=@CategoryName,[Description]=@Description,[Picture]=@Picture WHERE [CategoryID]=@CategoryID" ConnectionString="<%$ ConnectionStrings:Northwind %>"></asp:SqlDataSource>
<asp:GridView id="GridView1" runat="server" DataKeyNames="CategoryID" AllowSorting="True" AllowPaging="True" DataSourceID="Source1"></asp:GridView>
</p>
<p>
</p>
<p>
<asp:Label id="CacheMsg" runat="server" AssociatedControlID="GridView1"></asp:Label>
</p>
</form>
</body>
</html>
<%@ Page Language="VB" Debug="True" %>
<%@ import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
' <snippet2>
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
' </snippet2>
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<p>
</p>
<p>
<asp:SqlDataSource id="Source1" runat="server" SelectCommand="SELECT * FROM [Categories]" UpdateCommand="UPDATE [Categories] SET [CategoryName]=@CategoryName,[Description]=@Description,[Picture]=@Picture WHERE [CategoryID]=@CategoryID" ConnectionString="<%$ ConnectionStrings:Northwind %>"></asp:SqlDataSource>
<asp:GridView id="GridView1" runat="server" DataKeyNames="CategoryID" AllowSorting="True" AllowPaging="True" DataSourceID="Source1"></asp:GridView>
</p>
<p>
</p>
<p>
<asp:Label id="CacheMsg" runat="server" AssociatedControlID="GridView1"></asp:Label>
</p>
</form>
</body>
</html>
注釈
サポートされているすべてのバージョンのSQL Server (Microsoft SQL Server 7.0、Microsoft SQL Server 2000、および SQL Server 2005) では、SqlCacheDependency クラスは特定のSQL Server データベース テーブルを監視します。 テーブルが変更されると、テーブルに関連付けられている項目が Cacheから削除され、新しいバージョンの項目が Cacheに追加されます。
SqlCacheDependency クラスは、SQL Server 2005 データベースを使用する場合、System.Data.SqlClient.SqlDependency クラスとの統合もサポートします。 SQL Server 2005 のクエリ通知メカニズムは、SQL クエリの結果を無効にするデータの変更を検出し、System.Web.Caching.Cacheから SQL クエリに関連付けられているキャッシュされた項目をすべて削除します。
SqlCacheDependency クラスを使用すると、SQL Server 2005 を使用する場合に、SQL Server データベース テーブルまたは SQL クエリに依存する項目をアプリケーションの Cache に追加できます。 このクラスを @ OutputCache ディレクティブと共に使用して、SQL Server データベース テーブルに依存する出力キャッシュ ページまたはユーザー コントロールを作成することもできます。 最後に、@ OutputCache ページ ディレクティブと共に SqlCacheDependency クラスを使用して、SQL Server 2005 を使用するときに、SQL クエリの結果に依存する出力キャッシュ ページを作成できます。 SQL Server 2005 を使用したクエリ通知は、ユーザー コントロールの @ OutputCache ディレクティブではサポートされていません。
Note
テーブル ベースの通知を使用するときにこのクラスを正しく機能させるには、依存関係を作成するデータベースとテーブルで通知が有効になっている必要があります。 通知を有効にするには、 SqlCacheDependencyAdmin クラスのメソッドを呼び出すか、 aspnet_regsql.exe コマンドライン ツールを使用します。 さらに、適切な構成設定は、アプリケーションの Web.config ファイルに含まれている必要があります。
SQL Server 2005 クエリ通知で SqlCacheDependency オブジェクトを使用する場合、明示的な構成は必要ありません。 クエリ通知を使用するときに許可される Transact-SQL クエリの種類に関する制限については、SQL Serverドキュメントを参照してください。
次の例は、SQL Server データベース テーブルに対するテーブル ベースの依存関係を有効にする ASP.NET Web.config ファイルを示しています。
<configuration>
<connectionStrings>
<add name="Northwind" connectionString="Data Source=(local); Initial Catalog=northwind; Integrated Security=true"; providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<caching>
<sqlCacheDependency enabled = "true" pollTime = "60000" >
<databases>
<add name="northwind"
connectionStringName="Northwind"
pollTime="9000000"
/>
</databases>
</sqlCacheDependency>
</caching>
</system.web>
</configuration>
コンストラクター
| 名前 | 説明 |
|---|---|
| SqlCacheDependency(SqlCommand) |
指定されたSqlCommandを使用してキャッシュ キーの依存関係を作成し、SqlCacheDependency クラスの新しいインスタンスを初期化します。 |
| SqlCacheDependency(String, String) |
指定されたパラメーターを使用してキャッシュ キーの依存関係を作成し、 SqlCacheDependency クラスの新しいインスタンスを初期化します。 |
プロパティ
| 名前 | 説明 |
|---|---|
| HasChanged |
CacheDependency オブジェクトが変更されたかどうかを示す値を取得します。 (継承元 CacheDependency) |
| UtcLastModified |
依存関係が最後に変更された時刻を取得します。 (継承元 CacheDependency) |
メソッド
| 名前 | 説明 |
|---|---|
| CreateOutputCacheDependency(String) |
ASP.NET アプリケーションの OutputCache オブジェクトに格納されている項目とSQL Server データベース テーブルの間に依存関係を作成します。 |
| DependencyDispose() |
CacheDependency クラスによって使用されるリソースと、CacheDependencyから派生するすべてのクラスを解放します。 (継承元 CacheDependency) |
| Dispose() |
CacheDependency オブジェクトによって使用されるリソースを解放します。 (継承元 CacheDependency) |
| Equals(Object) |
指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
| FinishInit() |
CacheDependency オブジェクトの初期化を完了します。 (継承元 CacheDependency) |
| GetFileDependencies() |
ファイルの依存関係を取得します。 (継承元 CacheDependency) |
| GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
| GetType() |
現在のインスタンスの Type を取得します。 (継承元 Object) |
| GetUniqueID() |
SqlCacheDependency オブジェクトの一意識別子を取得します。 |
| ItemRemoved() |
監視対象のキャッシュ エントリが削除されたときに呼び出されます。 (継承元 CacheDependency) |
| KeepDependenciesAlive() |
この項目に依存するすべてのキャッシュ 項目の最終アクセス時刻を更新します。 (継承元 CacheDependency) |
| MemberwiseClone() |
現在の Objectの簡易コピーを作成します。 (継承元 Object) |
| NotifyDependencyChanged(Object, EventArgs) |
派生CacheDependency クラスによって表される依存関係が変更されたことを基本CacheDependency オブジェクトに通知します。 (継承元 CacheDependency) |
| SetCacheDependencyChanged(Action<Object,EventArgs>) |
この依存関係に対する変更の関係者への通知を処理する Action メソッドを追加します。 (継承元 CacheDependency) |
| SetUtcLastModified(DateTime) |
依存関係が最後に変更された時刻をマークします。 (継承元 CacheDependency) |
| TakeOwnership() |
最初のユーザーがこの依存関係の排他的所有権を宣言できるようにします。 (継承元 CacheDependency) |
| ToString() |
現在のオブジェクトを表す文字列を返します。 (継承元 Object) |