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 コンストラクタ (String, String)
アセンブリ: System.Web (system.web.dll 内)

Dim databaseEntryName As String Dim tableName As String Dim instance As New SqlCacheDependency(databaseEntryName, tableName)

例外の種類 | 条件 |
---|---|
HttpException | SqlClientPermission の内部検証が失敗しました。 または テーブル ベースの通知用に設定されているデータベースの一覧に databaseEntryName がありませんでした。 または 初期化時に SqlCacheDependency オブジェクトをデータベースに接続できませんでした。 または SqlCacheDependency オブジェクトをサポートするデータベース ストアド プロシージャまたはデータベースのいずれかで、SqlCacheDependency オブジェクトが権限拒否エラーを検出しました。 |
ArgumentException | tableName パラメータが String.Empty です。 |
ConfigurationErrorsException | SqlCacheDependency でポーリングが有効になっていません。 または または アプリケーションの構成ファイルで接続文字列が指定されませんでした。 または アプリケーションの構成ファイルで指定されている接続文字列が見つかりませんでした。 または |
DatabaseNotEnabledForNotificationException | |
TableNotEnabledForNotificationException | |
ArgumentNullException | databaseEntryName が null 参照 (Visual Basic では Nothing) です。 または tableName が null 参照 (Visual Basic では Nothing) です。 |

このコンストラクタを使用して、SQL Server 7 製品および SQL Server 2000 製品の SqlCacheDependency オブジェクトを作成します。
database パラメータに渡されるデータベース名は、アプリケーションの Web.config ファイルで定義されている必要があります。例として、pubs というデータベースの SqlCacheDependency 変更通知を定義する 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>
このコンストラクタを使用する場合は、DatabaseNotEnabledForNotificationException および TableNotEnabledForNotificationException という 2 つの例外がスローされるのが一般的です。DatabaseNotEnabledForNotificationException がスローされた場合は、例外処理コードで SqlCacheDependencyAdmin.EnableNotifications メソッドを呼び出すか、コマンド ライン ツールの Aspnet_regsql.exe を使用して、データベースの通知を設定します。TableNotEnabledForNotificationException がスローされた場合は、SqlCacheDependencyAdmin.EnableTableForNotifications メソッドを呼び出すか、Aspnet_regsql.exe を使用して、テーブルの通知を設定します。

このコンストラクタを使用して、SQL Server データベース (Northwind) のデータベース テーブル (Categories) に関連付けられている SqlCacheDependency クラスのインスタンスを作成するコード例を次に示します。
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

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 コンストラクタ (SqlCommand)
アセンブリ: System.Web (system.web.dll 内)



このコンストラクタは、SQL Server 2005 製品のクエリ通知機能を使用する SqlCacheDependency オブジェクトの作成に使用します。
sqlCommand パラメータに関連付けられている SQL ステートメントには、次の要素が含まれる必要があります。
-
たとえば、データベース所有者に所有されている "customers" という名前のテーブルを参照するには、SQL ステートメントは "dbo.customers" を参照する必要があります。
-
SELECT ステートメント内の明示的な列名。アスタリスク (*) のワイルドカード文字を使用して、テーブルのすべての列を選択することはできません。
たとえば、"select * from customers" の代わりに "select name, address, city, state from dbo.customers" を使用する必要があります。
このコンストラクタによって、SQL Server 2005 のクエリ通知を使用してページ レベルの出力キャッシュを行うページ上の SqlCacheDependency インスタンスと SqlCommand インスタンスを関連付けることはできません。

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 コンストラクタ

名前 | 説明 |
---|---|
SqlCacheDependency (SqlCommand) | SqlCacheDependency クラスの新しいインスタンスを初期化し、指定されている SqlCommand を使用してキャッシュ キーの依存関係を作成します。 |
SqlCacheDependency (String, String) | SqlCacheDependency クラスの新しいインスタンスを初期化し、指定されているパラメータを使用してキャッシュ キーの依存関係を作成します。 |

SqlCacheDependency プロパティ

名前 | 説明 | |
---|---|---|
![]() | HasChanged | CacheDependency オブジェクトが変更されたかどうかを示す値を取得します。 ( CacheDependency から継承されます。) |
![]() | UtcLastModified | 依存関係が最後に変更された時刻を取得します。 ( CacheDependency から継承されます。) |

SqlCacheDependency メソッド

名前 | 説明 | |
---|---|---|
![]() | Dispose | CacheDependency オブジェクトに使用されているリソースを解放します。 ( CacheDependency から継承されます。) |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | GetUniqueID | オーバーライドされます。 SqlCacheDependency オブジェクトの一意の識別子を取得します。 |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

SqlCacheDependency メンバ
ASP.NET アプリケーションの Cache オブジェクトに格納されている項目と、特定の SQL Server データベース テーブルまたは SQL Server 2005 クエリの結果との間にリレーションシップを確立します。このクラスは継承できません。
SqlCacheDependency データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | HasChanged | CacheDependency オブジェクトが変更されたかどうかを示す値を取得します。(CacheDependency から継承されます。) |
![]() | UtcLastModified | 依存関係が最後に変更された時刻を取得します。(CacheDependency から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Dispose | CacheDependency オブジェクトに使用されているリソースを解放します。 (CacheDependency から継承されます。) |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | GetUniqueID | オーバーライドされます。 SqlCacheDependency オブジェクトの一意の識別子を取得します。 |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

Weblioに収録されているすべての辞書からSqlCacheDependencyを検索する場合は、下記のリンクをクリックしてください。

- SqlCacheDependencyのページへのリンク