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 且控制項所顯示 GridView 的任何資料會在下次輪詢資料庫時由 控制項 SqlDataSource 重新整理。

<%@ 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 驗證 下使用用戶端模擬時,會在第一位使用者存取資料時快取資料。 如果其他使用者要求相同的資料,則會從快取擷取資料。 不會透過對資料庫進行另一個呼叫來擷取資料,以驗證使用者對資料的存取權。 如果您預期有多個使用者存取資料,而且您希望每個擷取到資料庫的安全性組態驗證資料,請勿使用快取。

適用於

另請參閱