SqlCacheDependency コンストラクター
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
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 プロパティは true に設定され、@ OutputCache属性が 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を使用する必要があります。
このコンストラクターを使用して、SqlCommand インスタンスをページレベルの出力キャッシュを使用して、SQL Server 2005 クエリ通知を使用してページ上の SqlCacheDependency インスタンスに関連付けることはできません。
こちらもご覧ください
適用対象
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 パラメーターで指定されたデータベース テーブルは、変更通知に対して有効になっていません。
例
次のコード例では、このコンストラクターを使用して、Northwind という名前のSQL Server データベース内の Categories という名前のデータベース テーブルに関連付けられている SqlCacheDependency クラスのインスタンスを作成します。
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を使用して通知のテーブルを設定できます。