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
用于创建对象的对象SqlCommandSqlCacheDependency。
例外
参数 sqlCmd 为 null.
该SqlCommand实例的属性设置为 NotificationAutoEnlist ,true并且页面上有一个@ OutputCache指令,SqlDependency其属性设置为 CommandNotification。
注解
此构造函数用于创建使用 SQL Server 2005 产品的查询通知功能的 SqlCacheDependency 对象。
与 sqlCmd 参数关联的 SQL 语句必须包含以下项:
完全限定的表名,包括表所有者的名称。 例如,若要引用数据库所有者拥有的名为 Customers 的表,SQL 语句必须引用
dbo.customers。Select 语句中的显式列名。 不能使用星号 \ 通配符从表中选择所有列。 例如,必须使用
而不是 。
此构造函数不能用于将 SqlCommand 实例与页面上的 SqlCacheDependency 实例相关联,该实例使用SQL Server 2005 查询通知与页面级输出缓存。
另请参阅
适用于
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 数据库表。
示例
下面的代码示例使用此构造函数创建与名为 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。 如果引发 a DatabaseNotEnabledForNotificationException ,则可以在异常处理代码中调用 SqlCacheDependencyAdmin.EnableNotifications 该方法,或使用 aspnet_regsql.exe 命令行工具为通知设置数据库。 如果引发 a TableNotEnabledForNotificationException ,可以调用 SqlCacheDependencyAdmin.EnableTableForNotifications 该方法或使用 aspnet_regsql.exe 方法设置通知表。