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.02000、および 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

databaseEntryNamenull 参照 (Visual Basic では Nothing) です。

または

tableNamenull 参照 (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

その他の技術情報

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



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「SqlCacheDependency」の関連用語

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

   

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



SqlCacheDependencyのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS