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

属性值

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

适用于

另请参阅