SqlCacheDependency クラス

定義

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
継承
SqlCacheDependency

次のコード例では、and GridView コントロールをSqlDataSource使用してデータベース テーブルを表示します。 ページが読み込まれると、ページはオブジェクトの作成を SqlCacheDependency 試みます。 オブジェクトが SqlCacheDependency 作成されると、ページはオブジェクトへの Cache 依存関係を持つアイテムを SqlCacheDependency 追加します。 次に示すような例外処理を使用する必要があります。

<%@ 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 クエリの結果を無効にするデータの変更を検出し、SQL クエリに関連付けられているキャッシュされた項目を削除System.Web.Caching.Cacheします。

このクラスをSqlCacheDependency使用すると、SQL Server 2005 を使用する場合は、SQL Server データベース テーブルまたはSQL クエリに依存する項目をアプリケーションCacheに追加できます。 このクラスをディレクティブと共に@ OutputCache使用して、SQL Server データベース テーブルに依存する出力キャッシュ ページまたはユーザー コントロールを作成することもできます。 最後に、page ディレクティブと共にクラスをSqlCacheDependency@ OutputCache使用して、SQL Server 2005 を使用する場合に、SQL クエリの結果に依存する出力キャッシュ ページを作成できます。 SQL Server 2005 を使用したクエリ通知は、ユーザー コントロールの@ OutputCacheディレクティブではサポートされていません。

注意

テーブル ベースの通知を使用するときにこのクラスが正しく動作するには、データベースと依存関係を作成するすべてのテーブルで通知が有効になっている必要があります。 通知を有効にするには、クラスのメソッドを SqlCacheDependencyAdmin 呼び出すか、コマンド ライン ツールを aspnet_regsql.exe 使用します。 また、適切な構成設定は、アプリケーションのWeb.config ファイルに含まれている必要があります。

SqlCacheDependency SQL Server 2005 クエリ通知でオブジェクトを使用する場合、明示的な構成は必要ありません。 クエリ通知を使用するときに許可される 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)

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

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>)

変更において関係者への通知を処理するアクション メソッドをこの依存関係に追加します。

(継承元 CacheDependency)
SetUtcLastModified(DateTime)

依存関係が最後に変更された時刻にマークを付けます。

(継承元 CacheDependency)
TakeOwnership()

この依存関係の排他的所有権を宣言する最初のユーザーを許可します。

(継承元 CacheDependency)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください