Share via


SqlCacheDependency コンストラクター

定義

SqlCacheDependency クラスの新しいインスタンスを初期化します。

オーバーロード

SqlCacheDependency(SqlCommand)

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

SqlCacheDependency(String, String)

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

SqlCacheDependency(SqlCommand)

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

public:
 SqlCacheDependency(System::Data::SqlClient::SqlCommand ^ sqlCmd);
public SqlCacheDependency (System.Data.SqlClient.SqlCommand sqlCmd);
new System.Web.Caching.SqlCacheDependency : System.Data.SqlClient.SqlCommand -> System.Web.Caching.SqlCacheDependency
Public Sub New (sqlCmd As SqlCommand)

パラメーター

sqlCmd
SqlCommand

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

例外

sqlCmd パラメーターが null です。

インスタンスの SqlCommand プロパティは NotificationAutoEnlist に設定され、 属性が @ OutputCachetrue設定されたディレクティブがページSqlDependencyに存在しますCommandNotification

注釈

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

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

  • テーブル所有者の名前を含む完全修飾テーブル名。 たとえば、データベース所有者が所有する Customers という名前のテーブルを参照するには、SQL ステートメントで を参照する dbo.customers必要があります。

  • Select ステートメントの明示的な列名。 アスタリスク (*) ワイルドカード文字を使用して、テーブルからすべての列を選択することはできません。 たとえば、 の select * from dbo.customers代わりに を使用 select name, address, city, state from dbo.customersする必要があります。

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

こちらもご覧ください

適用対象

SqlCacheDependency(String, String)

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

public:
 SqlCacheDependency(System::String ^ databaseEntryName, System::String ^ tableName);
public SqlCacheDependency (string databaseEntryName, string tableName);
new System.Web.Caching.SqlCacheDependency : string * string -> System.Web.Caching.SqlCacheDependency
Public Sub New (databaseEntryName As String, tableName As String)

パラメーター

databaseEntryName
String

アプリケーションの Web.config ファイルの databases 要素で定義されているデータベースの名前。

tableName
String

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

例外

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

- または -

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

- または -

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

- または -

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

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

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

- または -

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

- または -

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

- または -

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

- または -

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

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

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

databaseEntryNamenullです。

または

tableNamenull です。

次のコード例では、このコンストラクターを使用して、Northwind という名前のSqlCacheDependencySQL Server データベースに Categories という名前のデータベース テーブルに関連付けられている クラスのインスタンスを作成します。

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."; 
    } 
} 
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

注釈

このコンストラクターは、SQL Server 7.0 および SQL Server 2000 製品のオブジェクトを作成SqlCacheDependencyするために使用されます。

パラメーターに database 渡されるデータベース名は、アプリケーションの Web.config ファイルで定義する必要があります。 たとえば、次のWeb.config ファイルでは、変更通知用に pubs という名前の SqlCacheDependency データベースを定義しています。

<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"
            connectionStringName="pubs"
            pollTime="9000000"
            />
        </databases>
      </sqlCacheDependency>
    </caching>
  </system.web>
</configuration>

このコンストラクターを使用 DatabaseNotEnabledForNotificationException すると、一般的に と TableNotEnabledForNotificationExceptionの 2 つの例外がスローされます。 DatabaseNotEnabledForNotificationExceptionがスローされた場合は、例外処理コードで メソッドをSqlCacheDependencyAdmin.EnableNotifications呼び出すか、コマンド ライン ツールをaspnet_regsql.exe使用して通知用にデータベースを設定できます。 TableNotEnabledForNotificationExceptionがスローされた場合は、 メソッドを呼び出すか、 をSqlCacheDependencyAdmin.EnableTableForNotifications使用aspnet_regsql.exeして通知用のテーブルを設定できます。

こちらもご覧ください

適用対象