SqlDataSource.SqlCacheDependency SqlDataSource.SqlCacheDependency SqlDataSource.SqlCacheDependency SqlDataSource.SqlCacheDependency Property

定义

获取或设置一个用分号分隔的字符串,指示用于 Microsoft SQL Server 缓存依赖项的数据库和表。Gets or sets a semicolon-delimited string that indicates which databases and tables to use for the Microsoft SQL Server cache dependency.

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 缓存依赖项的数据库和表。A string that indicates which databases and tables to use for the SQL Server cache dependency.

示例

下面的代码示例演示如何创建 SQL Server 缓存依赖项并设置SqlCacheDependency SqlDataSource控件的属性。The following code example demonstrates how to create a SQL Server cache dependency and set the SqlCacheDependency property of a SqlDataSource control. 在此示例中, 数据库每120秒轮询一次。In this example, the database is polled every 120 seconds. 如果 Northwind 商贸 Employees 表中的数据在这段时间内发生更改, 则由该控件缓存SqlDataSourceGridView由控件显示的任何数据将在下SqlDataSource一次数据库轮询时由控件刷新。If the data in the Northwind Traders Employees table changes during that time, any data that is cached by the SqlDataSource control and displayed by the GridView control is refreshed by the SqlDataSource control the next time the database is polled.

<%@ 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>

注解

控件支持基于SqlCacheDependency数据缓存的对象的可选过期策略 (必须为数据库服务器配置服务)。 SqlDataSourceThe SqlDataSource control supports an optional expiration policy based on the SqlCacheDependency object for the data cache (the service must be configured for the database server).

字符串根据@ Page指令使用的相同格式标识数据库和表, 其中字符串的第一部分是 Microsoft SQL Server 数据库的连接字符串, 后跟冒号分隔符, SqlCacheDependency最后是数据库表的名称 (例如"connectionstring1:table1")。The SqlCacheDependency string identifies databases and tables according to the same format that is used by the @ Page directive, where the first part of the string is a connection string to a Microsoft SQL Server database, followed by a colon delimiter, and finally the name of the database table (for example, "connectionstring1:table1"). 如果属性依赖于多个表, 则连接字符串和表名称对由分号分隔 ( "connectionstring1:table1";connectionstring2:table2"例如)。 SqlCacheDependencyIf the SqlCacheDependency property depends on more than one table, the connection string-and-table name pairs are separated by semicolons (for example, "connectionstring1:table1";connectionstring2:table2").

重要

使用 Microsoft Windows 身份验证下的客户端模拟时, 将在第一个用户访问数据时缓存数据。When you are using client impersonation under Microsoft Windows authentication, the data is cached when the first user accesses the data. 如果其他用户请求相同的数据, 则从缓存中检索数据。If another user requests the same data, the data is retrieved from the cache. 通过对数据库进行另一次调用以验证用户对数据的访问权限, 不会检索数据。The data is not retrieved by making another call to the database to verify the user's access to the data. 如果希望有多个用户访问数据, 并且希望数据库的安全配置验证每个数据的检索, 请不要使用缓存。If you expect more than one user to access the data, and you want each retrieval to the data to be verified by the security configurations for the database, do not use caching.

适用于

另请参阅