SqlDataSource.SqlCacheDependency 属性

定义

获取或设置一个用分号分隔的字符串,指示用于 Microsoft SQL Server 缓存依赖项的数据库和表。

public:
 virtual property System::String ^ SqlCacheDependency { System::String ^ get(); void set(System::String ^ value); };
public virtual string SqlCacheDependency { get; set; }
member this.SqlCacheDependency : string with get, set
Public Overridable Property SqlCacheDependency As String

属性值

一个字符串,指示用于 SQL Server 缓存依赖项的数据库和表。

示例

下面的代码示例演示如何创建 SQL Server 缓存依赖项并设置 SqlCacheDependency 控件的 SqlDataSource 属性。 在此示例中,数据库每 120 秒轮询一次。 如果 Northwind Traders Employees 表中的数据在此期间发生更改,则控件在下次轮询数据库时,控件将刷新SqlDataSource控件缓存SqlDataSource并由控件显示GridView的任何数据。

<%@ Page language="c#" %>

<!--

The page uses an example configuration that includes
connection strings and a defined SqlCacheDependecy.

<?xml version="1.0"?>
<configuration>

  <connectionStrings>
    <add name="MyNorthwind"
         connectionString="Data Source="localhost";Integrated Security="SSPI";Initial Catalog="Northwind""
         providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    <caching>
      <sqlCacheDependency enabled="true">
        <databases>
          <add
            name="Northwind"
            connectionStringName="MyNorthwind"
            pollTime="120000" />
        </databases>
      </sqlCacheDependency>
    </caching>

  </system.web>
</configuration>
-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="Form1" method="post" runat="server">

        <asp:gridview
          id="GridView1"
          runat="server"
          datasourceid="SqlDataSource1" />

        <asp:sqldatasource
          id="SqlDataSource1"
          runat="server"
          connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
          selectcommand="SELECT EmployeeID,FirstName,Lastname FROM Employees"
          enablecaching="True"
          cacheduration="300"
          cacheexpirationpolicy="Absolute"
          sqlcachedependency="Northwind:Employees" />

    </form>
  </body>
</html>
<%@ Page language="vb" %>

<!--

The page uses an example configuration that includes
connection strings and a defined SqlCacheDependecy.

<?xml version="1.0"?>
<configuration>

  <connectionStrings>
    <add name="MyNorthwind"
         connectionString="Data Source="localhost";Integrated Security="SSPI";Initial Catalog="Northwind""
         providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    <caching>
      <sqlCacheDependency enabled="true">
        <databases>
          <add
            name="Northwind"
            connectionStringName="MyNorthwind"
            pollTime="120000" />
        </databases>
      </sqlCacheDependency>
    </caching>

  </system.web>
</configuration>
-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="Form1" method="post" runat="server">

        <asp:gridview
          id="GridView1"
          runat="server"
          datasourceid="SqlDataSource1" />

        <asp:sqldatasource
          id="SqlDataSource1"
          runat="server"
          connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
          selectcommand="SELECT EmployeeID,FirstName,Lastname FROM Employees"
          enablecaching="True"
          cacheduration="300"
          cacheexpirationpolicy="Absolute"
          sqlcachedependency="Northwind:Employees" />

    </form>
  </body>
</html>

注解

控件 SqlDataSource 支持基于 SqlCacheDependency 数据缓存的 对象的可选过期策略, (必须为数据库服务器) 配置服务。

字符串 SqlCacheDependency 根据 指令使用的 @ Page 相同格式标识数据库和表,其中字符串的第一部分是 Microsoft SQL Server 数据库的连接字符串,后跟冒号分隔符,最后是数据库表的名称 (例如 "connectionstring1:table1" ,) 。 SqlCacheDependency如果 属性依赖于多个表,则连接字符串和表名称对用分号分隔 (例如 "connectionstring1:table1";connectionstring2:table2" ,) 。

重要

在 Microsoft Windows 身份验证下使用客户端模拟时,当第一个用户访问数据时,数据将缓存。 如果其他用户请求相同的数据,则会从缓存中检索数据。 通过再次调用数据库来验证用户对数据的访问权限,不会检索数据。 如果希望多个用户访问数据,并且希望对数据的每次检索都由数据库的安全配置进行验证,请不要使用缓存。

适用于

另请参阅