更新:2007 年 11 月
在以下两者之间建立关系:一是在 ASP.NET 应用程序的 Cache 对象中存储的项;二是特定 SQL Server 数据库表或 SQL Server 2005 查询的结果。无法继承此类。
命名空间:
System.Web.Caching 程序集:
System.Web(在 System.Web.dll 中)
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class SqlCacheDependency _
Inherits CacheDependency
Dim instance As SqlCacheDependency
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class SqlCacheDependency : CacheDependency
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class SqlCacheDependency sealed : public CacheDependency
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal) */
public final class SqlCacheDependency extends CacheDependency
public final class SqlCacheDependency extends CacheDependency
在所有受支持的 SQL Server 版本(Microsoft SQL Server 7.0、Microsoft SQL Server 2000 和 SQL Server 2005)上,SqlCacheDependency 类监视特定 SQL Server 数据库表。当该表更改时,将从 Cache 中移除与该表关联的项,并向 Cache 中添加该项的新版本。
在使用 SQL Server 2005 数据库时,SqlCacheDependency 类还支持与 System.Data.SqlClient..::.SqlDependency 类的集成。SQL Server 2005 的查询通知机制检测使 SQL 查询结果无效的数据更改,并从 System.Web.Caching..::.Cache 中移除与该 SQL 查询关联的任何缓存项。
在使用 SQL Server 2005 时,可以使用 SqlCacheDependency 类向应用程序的 Cache 中添加依赖于 SQL Server 数据库表或 SQL 查询的项。还可以将此类与 @ OutputCache 指令一起使用,以生成依赖于 SQL Server 数据库表的输出缓存的页或用户控件。最后,在使用 SQL Server 2005 时,可以将 SqlCacheDependency 类与 @ OutputCache 页指令一起使用,以生成依赖于 SQL 查询结果的输出缓存的页。对于用户控件,@ OutputCache 指令不支持使用 SQL Server 2005 的查询通知。
说明: |
|---|
为使此类在使用基于表的通知时正常工作,必须为数据库及要设置依赖项的任何表启用通知。可通过调用 SqlCacheDependencyAdmin 类的方法或使用 Aspnet_regsql.exe 命令行工具启用通知。此外,应用程序的 Web.config 文件中必须包含正确的配置设置。 将 SqlCacheDependency 对象与 SQL Server 2005 查询通知一起使用不需要任何显式配置。有关使用查询通知时允许的 Transact-SQL 查询类型限制的信息,请参考 SQL Server 2005 联机丛书。 |
下面的示例演示 ASP.NET Web.config 文件,该文件对 SQL Server 数据库表启用了基于表的依赖项。
<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>
下面的代码示例使用 SqlDataSource 和 GridView 控件显示数据库表。加载页时,该页试图创建 SqlCacheDependency 对象。创建 SqlCacheDependency 对象后,该页向 Cache 中添加一个项,该项在 SqlCacheDependency 对象上有一个依赖项。应使用与此处所示类似的异常处理。
<%@ 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">
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
</script>
<html >
<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>
System..::.Object
System.Web.Caching..::.CacheDependency
System.Web.Caching..::.SqlCacheDependency
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。
.NET Framework
受以下版本支持:3.5、3.0、2.0
参考
其他资源