Bewerken

Delen via


SqlDataSource.CacheExpirationPolicy Property

Definition

Gets or sets the cache expiration behavior that, when combined with the duration, describes the behavior of the cache that the data source control uses.

public:
 virtual property System::Web::UI::DataSourceCacheExpiry CacheExpirationPolicy { System::Web::UI::DataSourceCacheExpiry get(); void set(System::Web::UI::DataSourceCacheExpiry value); };
public virtual System.Web.UI.DataSourceCacheExpiry CacheExpirationPolicy { get; set; }
member this.CacheExpirationPolicy : System.Web.UI.DataSourceCacheExpiry with get, set
Public Overridable Property CacheExpirationPolicy As DataSourceCacheExpiry

Property Value

One of the DataSourceCacheExpiry values. The default is the Absolute value.

Examples

The following code example demonstrates how to retrieve data from the Northwind database in Microsoft SQL Server and display it in a GridView control, with data caching enabled. Because the CacheExpirationPolicy property is set to the Sliding value, users will not see fresh data even though the CacheDuration value is 20 seconds. The cached data is refreshed only if there is no activity on the page for more than 20 seconds since the last page load.

<!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"
                CacheExpirationPolicy="Sliding"
                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>
<!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"
                CacheExpirationPolicy="Sliding"
                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>

Remarks

The SqlDataSource control supports data caching. While data is cached, the Select method retrieves data from the cache rather than from the underlying database. When the cache expires, the Select method retrieves data from the underlying database, and then caches the data again.

The behavior of the cache is determined by a combination of the CacheDuration and CacheExpirationPolicy settings. If the CacheExpirationPolicy property is set to the Absolute value, the SqlDataSource caches data on the first data retrieval operation, holds it in memory for the amount of time that is specified by the CacheDuration property, and then discards it after the time has lapsed. The cache is then refreshed during the next operation. If the CacheExpirationPolicy property is set to the Sliding value, the data source control caches data on the first data retrieval operation, but resets the time window that it holds the cache for each subsequent operation. The cache expires, if there is no activity for a time that is equal to the CacheDuration value since the last Select operation.

The SqlDataSource control can cache data only when in the DataSet mode. A NotSupportedException exception is thrown by the Select method, if the SqlDataSource control is set to the DataReader value and caching is also enabled.

Important

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.

Applies to

See also