请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
先前版本

  开启低带宽视图
此页面仅适用于
Microsoft Visual Studio 2005/.NET Framework 2.0

同时提供下列产品的其他版本:
.NET Framework 类库
SqlCacheDependency 类

注意:此类在 .NET Framework 2.0 版中是新增的。

在以下两者之间建立关系:一是在 ASP.NET 应用程序的 Cache 对象中存储的项;二是特定 SQL Server 数据库表或 SQL Server 2005 查询的结果。无法继承此类。

命名空间:System.Web.Caching
程序集:System.Web(在 system.web.dll 中)

Visual Basic(声明)
Public NotInheritable Class SqlCacheDependency
    Inherits CacheDependency
Visual Basic(用法)
Dim instance As SqlCacheDependency
C#
public sealed class SqlCacheDependency : CacheDependency
C++
public ref class SqlCacheDependency sealed : public CacheDependency
J#
public final class SqlCacheDependency extends CacheDependency
JScript
public final class SqlCacheDependency extends CacheDependency

SqlCacheDependency 类在所有受支持的 SQL Server 版本 (7.0, 2000, 2005) 上监视特定的 SQL Server 数据库表,以便在该表发生更改时,自动从 Cache 中删除与该表关联的项。

数据库表发生更改时,将自动删除缓存项,并向 Cache 中添加新版本的项。

在使用 SQL Server 2005 数据库时,SqlCacheDependency 类还支持与 System.Data.SqlClient.SqlDependency 类进行集成。使用 SQL Server 2005 的查询通知机制来检测使 SQL 查询结果无效的数据更改。与 SQL 查询关联的任何缓存项都将从 System.Web.Caching.Cache 中移除。

在使用 SQL Server 2005 时,可以使用 SqlCacheDependency 类向应用程序的 Cache 添加依赖于 SQL Server 数据库表或 SQL 查询的项。还可以将此类与 @ OutputCache 指令一起使用,以生成依赖于 SQL Server 数据库表的输出缓存的页或用户控件。最后,在使用 SQL Server 2005 时,可以将 SqlCacheDependency 类与 @ OutputCache 页指令一起使用,以生成依赖于 SQL 查询结果的输出缓存的页。对于用户控件,@ OutputCache 指令不支持使用 SQL Server 2005 的查询通知。

Note注意

为使此类在使用基于表的通知时正常工作,必须为数据库及要设置依赖项的任何表启用通知。可通过使用 SqlCacheDependencyAdmin 类的方法或 Aspnet_regsql.exe 命令行工具启用通知。同时,应用程序的 Web.config 文件中必须包含正确的配置设置。

SqlCacheDependency 对象与 SQL Server 2005 查询通知一起使用不需要任何显式配置。开发人员应参考 SQL Server 2005 联机丛书,以了解使用查询通知时允许的 Transact-SQL 查询类型的限制。

下面是一个 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" 
            connectionString = "pubs"
            pollTime = "9000000"
            />
        </databases>
      </sqlCacheDependency>
    </caching>
  </system.web>
</configuration>
TopicLocation
演练:将 ASP.NET 输出缓存与 SQL Server 结合使用在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:将 ASP.NET 输出缓存与 SQL Server 结合使用在 Visual Studio 中生成 ASP .NET Web 应用程序

下面的代码示例使用 SqlDataSourceGridView 控件显示数据库表。加载页时,该页试图创建 SqlCacheDependency 对象。创建 SqlCacheDependency 对象后,该页向 Cache 添加一个项,该项在 SqlCacheDependency 对象上有一个依赖项。应使用与此处所示类似的异常处理。

Visual Basic
<%@ Page Language="VB" Debug="True" %>
<%@ import Namespace="System.Data.SqlClient" %>
<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>
</head>
<body>
    <form 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="server='localhost';user id='bjctest';password='bjctest'; Database='Northwind'"></asp:SqlDataSource>
            <asp:GridView id="GridView1" runat="server" KeyFieldNames="CategoryID" AllowSorting="True" AllowPaging="True" DataSourceID="Source1"></asp:GridView>
        </p>
        <p>
        </p>
        <p>
            <asp:Label id="CacheMsg" runat="server"></asp:Label>
        </p>
   </form>
</body>
</html>
System.Object
   System.Web.Caching.CacheDependency
    System.Web.Caching.SqlCacheDependency
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利  |  商标  |  隐私权声明
Page view tracker