SqlDataSource.CacheDuration 屬性

定義

取得或設定資料來源控制項快取 Select(DataSourceSelectArguments) 方法所擷取之資料的時間長度 (以秒為單位)。

public:
 virtual property int CacheDuration { int get(); void set(int value); };
[System.ComponentModel.TypeConverter(typeof(System.Web.UI.DataSourceCacheDurationConverter))]
public virtual int CacheDuration { get; set; }
[<System.ComponentModel.TypeConverter(typeof(System.Web.UI.DataSourceCacheDurationConverter))>]
member this.CacheDuration : int with get, set
Public Overridable Property CacheDuration As Integer

屬性值

SqlDataSource 快取 Select(DataSourceSelectArguments) 作業結果的秒數。 預設值是 0。 值不能為負。

屬性

範例

下列程式碼範例示範如何在 Microsoft SQL Server 中從 Northwind 資料庫擷取資料,並在控制項中 GridView 顯示資料,並啟用資料快取。 設定 CacheDuration 為 20 秒,而且 CacheExpirationPolicy 屬性預設會設定為 Absolute 值,因此此頁面的使用者每隔 20 秒就會看到新的資料。

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

<!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" runat="server">

            <asp:SqlDataSource
                id="SqlDataSource1"
                runat="server"
                DataSourceMode="DataSet"
                ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
                EnableCaching="True"
                CacheDuration="20"
                SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees">
            </asp:SqlDataSource>

            <asp:GridView
                id="GridView1"
                runat="server"
                AutoGenerateColumns="False"
                DataSourceID="SqlDataSource1">
                <columns>
                    <asp:BoundField HeaderText="First Name" DataField="FirstName" />
                    <asp:BoundField HeaderText="Last Name" DataField="LastName" />
                    <asp:BoundField HeaderText="Title" DataField="Title" />
                </columns>
            </asp:GridView>

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

<!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" runat="server">

            <asp:SqlDataSource
                id="SqlDataSource1"
                runat="server"
                DataSourceMode="DataSet"
                ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
                EnableCaching="True"
                CacheDuration="20"
                SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees">
            </asp:SqlDataSource>

            <asp:GridView
                id="GridView1"
                runat="server"
                AutoGenerateColumns="False"
                DataSourceID="SqlDataSource1">
                <columns>
                    <asp:BoundField HeaderText="First Name" DataField="FirstName" />
                    <asp:BoundField HeaderText="Last Name" DataField="LastName" />
                    <asp:BoundField HeaderText="Title" DataField="Title" />
                </columns>
            </asp:GridView>

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

備註

控制項 SqlDataSource 支援資料快取。 快取資料時, Select 方法會從快取擷取資料,而不是從基礎資料庫擷取資料。 當快取過期時, Select 方法會從基礎資料庫擷取資料,然後再次快取資料。

SqlDataSource 屬性設定 true 為 且 CacheDuration 屬性設定為大於 0 的值時 EnableCaching ,控制項會自動快取資料,這表示快取在捨棄快取專案之前儲存資料的秒數。 值為 0 表示無限長快取。

快取的行為取決於持續時間和 CacheExpirationPolicy 屬性的組合。 CacheExpirationPolicy如果屬性設定為 Absolute 值,則會 SqlDataSource 快取第一個資料擷取作業上的資料,在記憶體中最多保留屬性所 CacheDuration 指定的時間量。 如果需要記憶體,資料可能會在持續時間之前釋放。 接著會在下一個作業期間重新整理快取。 CacheExpirationPolicy如果 屬性設定為 Sliding 值,資料來源控制項會在第一個資料擷取作業上快取資料,但會重設它保留每個後續作業快取的時間範圍。 如果上次作業之後的值等於 CacheDurationSelect ,快取就會過期。

控制項 SqlDataSource 只能在處於 DataSet 模式時快取資料。 NotSupportedException如果 SqlDataSource 控制項設定為 DataReader 值,而且也會啟用快取,則方法會擲 Select 回例外狀況。

重要

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

適用於

另請參閱