SqlCacheDependency 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 SqlCacheDependency 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| 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 A,用來創造一個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 檔案中資料庫元素中定義的資料庫名稱。
- tableName
- String
與 相關 SqlCacheDependency 聯的資料庫資料表名稱。
例外狀況
內部檢查 SqlClientPermission 失敗了。
-或-
在設定為資料表式通知的資料庫清單中找不到 。databaseEntryName
-或-
SqlCacheDependency物件在初始化時無法連接到資料庫。
-或-
該 SqlCacheDependency 物件在資料庫或支援該 SqlCacheDependency 物件的資料庫儲存程序上遇到權限拒絕錯誤。
參數 tableName 為 Empty。
對於 , SqlCacheDependency投票功能未啟用。
-或-
投票間隔設定不正確。
-或-
應用程式的設定檔中未指定 連接字串。
-或-
找不到應用程式設定檔中指定的 連接字串。
-或-
應用程式設定檔中指定的 連接字串 是空字串。
參數中指定的 databaseEntryName 資料庫並未啟用以發送變更通知。
參數中指定的 tableName 資料庫資料表並未啟用以發送變更通知。
範例
以下程式碼範例使用此建構子建立一個 SqlCacheDependency 類別實例,該類別與一個名為 Categories 的資料庫資料表相關聯,該資料表位於一個名為 Northwind 的SQL Server資料庫中。
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。 如果拋出 a DatabaseNotEnabledForNotificationException ,你可以用例外處理程式碼呼叫該 SqlCacheDependencyAdmin.EnableNotifications 方法,或使用 aspnet_regsql.exe 命令列工具設定資料庫以接收通知。 如果拋出 a TableNotEnabledForNotificationException ,你可以呼叫該 SqlCacheDependencyAdmin.EnableTableForNotifications 方法或 use aspnet_regsql.exe 來設定通知表。