SqlCacheDependency クラス
アセンブリ: System.Web (system.web.dll 内)


サポートされているバージョンのすべての SQL Server (7.0、2000、および 2005) では、SqlCacheDependency クラスが特定の SQL Server データベース テーブルを監視しているため、テーブルが変更されると、それに関連付けられている項目は Cache から自動的に削除されます。
データベース テーブルが変更されると、キャッシュされた項目は自動的に削除され、新しいバージョンの項目が Cache に追加されます。
SQL Server 2005 データベースを使用している場合、SqlCacheDependency クラスは System.Data.SqlClient.SqlDependency クラスとの統合もサポートします。SQL Server 2005 のクエリ通知機構を使用して、SQL クエリの結果を無効にするデータ変更を検出します。SQL クエリに関連付けられているキャッシュされた項目は、すべて System.Web.Caching.Cache から削除されます。
SQL Server 2005 を使用している場合は、SqlCacheDependency クラスを使用して、SQL Server データベース テーブルまたは SQL クエリに依存する項目をアプリケーションの Cache に追加できます。また、このクラスを @ OutputCache ディレクティブと共に使用して、出力キャッシュ ページまたは SQL Server データベース テーブルに依存するユーザー コントロールを作成することもできます。最後に、SQL Server 2005 を使用している場合は、SqlCacheDependency クラスを @ OutputCache ページ ディレクティブと共に使用して、SQL クエリの結果に依存する出力キャッシュ ページを作成できます。SQL Server 2005 を使用するクエリ通知は、ユーザー コントロールの @ OutputCache ディレクティブではサポートされません。
![]() |
---|
テーブル ベースの通知を使用する場合にこのクラスが正しく動作するには、依存させるデータベースおよびテーブルで通知が有効になっていることが必要です。通知は、SqlCacheDependencyAdmin クラスのメソッドまたは Aspnet_regsql.exe コマンド ライン ツールを使用して有効にできます。また、アプリケーションの Web.config ファイルで適切な構成が設定されていることも必要です。 SQL Server 2005 クエリ通知で SqlCacheDependency オブジェクトを使用する場合は、明示的な構成を行う必要はありません。開発者は、クエリ通知を使用する場合に利用できる Transact-SQL クエリの種類の制約事項について、『SQL Server 2005 Books Online』を参照しておく必要があります。 |
SQL Server データベース テーブルへのテーブル ベースの依存関係を有効にする、ASP.NET Web.config ファイルのサンプルを次に示します。
<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" connectionString = "pubs" pollTime = "9000000" /> </databases> </sqlCacheDependency> </caching> </system.web> </configuration>

SqlDataSource コントロールと GridView コントロールを使用してデータベース テーブルを表示するコード例を次に示します。ページは読み込まれると、SqlCacheDependency オブジェクトを作成しようとします。SqlCacheDependency オブジェクトを作成したら、ページは SqlCacheDependency オブジェクトに依存する項目を Cache に追加します。ここに示すものと同じような例外処理を使用してください。
<%@ Page Language="VB" Debug="True" %> <%@ import Namespace="System.Data.SqlClient" %> <script runat="server"> 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 </script> <html> <head> </head> <body> <form 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="server='localhost';user id='bjctest';password='bjctest'; Database='Northwind'"></asp:SqlDataSource> <asp:GridView id="GridView1" runat="server" KeyFieldNames="CategoryID" AllowSorting="True" AllowPaging="True" DataSourceID="Source1"></asp:GridView> </p> <p> </p> <p> <asp:Label id="CacheMsg" runat="server"></asp:Label> </p> </form> </body> </html>

System.Web.Caching.CacheDependency
System.Web.Caching.SqlCacheDependency


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- SqlCacheDependency クラスのページへのリンク