Export (0) Print
Expand All

SqlDataSource.CacheDuration Property

Gets or sets the length of time, in seconds, that the data source control caches data that is retrieved by the Select method.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

[TypeConverterAttribute(typeof(DataSourceCacheDurationConverter))]
public virtual int CacheDuration { get; set; }
<asp:SqlDataSource CacheDuration="Int32" />

Property Value

Type: System.Int32
The number of seconds that the SqlDataSource caches the results of a Select operation. The default is 0. The value cannot be negative.

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 SqlDataSource control automatically caches data when the EnableCaching property is set to true and the CacheDuration property is set to a value greater than 0, which indicates the number of seconds that the cache stores data before the cache entry is discarded. A value of 0 indicates an infinitely long cache.

The behavior of the cache is determined by a combination of the duration and the CacheExpirationPolicy property. 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, at most, the amount of time that is specified by CacheDuration property. The data might be released before the duration time, if the memory is required. 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 it is in 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.

Security noteSecurity Note

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.

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. The CacheDuration is set to 20 seconds and, because the CacheExpirationPolicy property is set to the Absolute value by default, users of this page will see fresh data every 20 seconds.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft