SqlCacheDependencyとは? わかりやすく解説

SqlCacheDependency クラス

メモ : このクラスは、.NET Framework version 2.0 で新しく追加されたものです。

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

名前空間: System.Web.Caching
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public NotInheritable Class
 SqlCacheDependency
    Inherits CacheDependency
Dim instance As SqlCacheDependency
public sealed class SqlCacheDependency : CacheDependency
public ref class SqlCacheDependency sealed
 : public CacheDependency
public final class SqlCacheDependency extends
 CacheDependency
public final class SqlCacheDependency extends
 CacheDependency
解説解説

サポートされているバージョンのすべての 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 ディレクティブではサポートされません。

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.Object
   System.Web.Caching.CacheDependency
    System.Web.Caching.SqlCacheDependency
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

SqlCacheDependency コンストラクタ (String, String)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

SqlCacheDependency クラスの新しいインスタンスを初期化し、指定されているパラメータを使用してキャッシュ キーの依存関係を作成します。

名前空間: System.Web.Caching
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public Sub New ( _
    databaseEntryName As String, _
    tableName As String _
)
Dim databaseEntryName As String
Dim tableName As String

Dim instance As New SqlCacheDependency(databaseEntryName,
 tableName)
public SqlCacheDependency (
    string databaseEntryName,
    string tableName
)
public:
SqlCacheDependency (
    String^ databaseEntryName, 
    String^ tableName
)
public SqlCacheDependency (
    String databaseEntryName, 
    String tableName
)
public function SqlCacheDependency (
    databaseEntryName : String, 
    tableName : String
)

パラメータ

databaseEntryName

アプリケーションの Web.config ファイルの caching の sqlCacheDependency の databases 要素 (ASP.NET 設定スキーマ) 要素で定義されているデータベースの名前。

tableName

SqlCacheDependency が関連付けられているデータベース テーブルの名前。

例外例外
例外の種類条件

HttpException

SqlClientPermission の内部検証が失敗しました。

または

テーブル ベースの通知用に設定されているデータベースの一覧に databaseEntryName がありませんでした。

または

初期化時に SqlCacheDependency オブジェクトをデータベースに接続できませんでした。

または

SqlCacheDependency オブジェクトをサポートするデータベース ストアド プロシージャまたはデータベースのいずれかで、SqlCacheDependency オブジェクトが権限拒否エラーを検出しました。

ArgumentException

tableName パラメータが String.Empty です。

ConfigurationErrorsException

SqlCacheDependency でポーリングが有効になっていません。

または

ポーリング間隔が正しく設定されていません。

または

アプリケーションの構成ファイルで接続文字列が指定されませんでした。

または

アプリケーションの構成ファイルで指定されている接続文字列が見つかりませんでした。

または

アプリケーションの構成ファイルで指定されている接続文字列が空の文字列です。

DatabaseNotEnabledForNotificationException

databaseEntryName パラメータで指定されているデータベースの変更通知が有効になっていません。

TableNotEnabledForNotificationException

tableName パラメータで指定されているデータベース テーブルの変更通知が有効になっていません。

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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SqlCacheDependency クラス
SqlCacheDependency メンバ
System.Web.Caching 名前空間
SqlCacheDependency

SqlCacheDependency コンストラクタ (SqlCommand)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

SqlCacheDependency クラスの新しいインスタンスを初期化し、指定されている SqlCommand を使用してキャッシュ キーの依存関係を作成します。

名前空間: System.Web.Caching
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public Sub New ( _
    sqlCmd As SqlCommand _
)
Dim sqlCmd As SqlCommand

Dim instance As New SqlCacheDependency(sqlCmd)
public SqlCacheDependency (
    SqlCommand sqlCmd
)
public:
SqlCacheDependency (
    SqlCommand^ sqlCmd
)
public SqlCacheDependency (
    SqlCommand sqlCmd
)
public function SqlCacheDependency (
    sqlCmd : SqlCommand
)

パラメータ

sqlCmd

SqlCacheDependency オブジェクトを作成するために使用する SqlCommand。

例外例外
例外の種類条件

ArgumentNullException

sqlCmd パラメータが null 参照 (Visual Basic では Nothing) です。

HttpException

SqlCommand インスタンスの NotificationAutoEnlist プロパティは true に設定され、ページには SqlDependency 属性が CommandNotification に設定された @ OutputCache ディレクティブがあります。

解説解説

このコンストラクタは、SQL Server 2005 製品のクエリ通知機能を使用する SqlCacheDependency オブジェクトの作成に使用します。

sqlCommand パラメータに関連付けられている SQL ステートメントには、次の要素が含まれる必要があります。

このコンストラクタによって、SQL Server 2005 のクエリ通知を使用してページ レベルの出力キャッシュを行うページ上の SqlCacheDependency インスタンスと SqlCommand インスタンスを関連付けることはできません。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SqlCacheDependency クラス
SqlCacheDependency メンバ
System.Web.Caching 名前空間
SqlCacheDependency

SqlCacheDependency コンストラクタ

SqlCacheDependency クラスの新しいインスタンスを初期化します。
オーバーロードの一覧オーバーロードの一覧

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

関連項目

SqlCacheDependency クラス
SqlCacheDependency メンバ
System.Web.Caching 名前空間
SqlCacheDependency

SqlCacheDependency プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ HasChanged  CacheDependency オブジェクトが変更されたかどうかを示す値を取得します。 ( CacheDependency から継承されます。)
パブリック プロパティ UtcLastModified  依存関係が最後に変更された時刻を取得します。 ( CacheDependency から継承されます。)
参照参照

関連項目

SqlCacheDependency クラス
System.Web.Caching 名前空間
SqlCacheDependencyAdmin

その他の技術情報

アプリケーション データのキャッシュ

SqlCacheDependency メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
参照参照

関連項目

SqlCacheDependency クラス
System.Web.Caching 名前空間
SqlCacheDependencyAdmin

その他の技術情報

アプリケーション データのキャッシュ

SqlCacheDependency メンバ

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

SqlCacheDependency データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド SqlCacheDependency オーバーロードされます。 SqlCacheDependency クラスの新しいインスタンスを初期化します。
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ HasChanged  CacheDependency オブジェクトが変更されたかどうかを示す値を取得します。(CacheDependency から継承されます。)
パブリック プロパティ UtcLastModified  依存関係が最後に変更された時刻を取得します。(CacheDependency から継承されます。)
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

SqlCacheDependency クラス
System.Web.Caching 名前空間
SqlCacheDependencyAdmin

その他の技術情報

アプリケーション データのキャッシュ



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「SqlCacheDependency」の関連用語

SqlCacheDependencyのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



SqlCacheDependencyのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS