Udostępnij za pośrednictwem


Caching Data Using Data Source Controls

Data source controls provide data services to data-bound controls such as the GridView, FormView, and DetailsView controls. These services include caching data to help improve the performance of applications in which the data does not change frequently.

To cache data using the SqlDataSource or AccessDataSource controls, you must set the DataSourceMode property of those controls to DataSet. The ObjectDataSource control can cache objects returned by the underlying business object. However, you should not cache objects that hold resources or that maintain state that cannot be shared among multiple requests, such as an open DataReader object.

Enabling Caching with a Data Source Control

Caching is not enabled by default for data source controls, but you can enable it by setting the control's EnableCaching property to true. Cached data is refreshed based on the number of seconds you specify using the CacheDuration property.

You can further refine the behavior of a data source control's caching behavior by setting its CacheExpirationPolicy property. Setting the property's value to Absolute forces the cache to be refreshed when the CacheDuration value is exceeded. Setting the CacheExpirationPolicy property to Sliding refreshes the cache only if the CacheDuration value has been exceeded since the last time the cached item was accessed.

Important noteImportant Note:

It is recommended that you set the EnableCaching property to false when client impersonation is enabled and the results from the data source are retrieved based on the client identity. If caching is enabled, cached data for a single user can be viewed by all users and sensitive information could be exposed to an unwanted source. Client impersonation is enabled when the impersonate attribute of the identity configuration element is set to true and anonymous identification is disabled for the application at the Web server.

The following code example shows a SqlDataSource control configured to refresh data every 20 seconds:

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

Advanced Caching Scenarios

If you are using the SqlDataSource control with the System.Data.SqlClient provider, you can take advantage of the SqlCacheDependency class to ensure that cached data is refreshed only when the source table in the database has been modified. For more information, see Caching Data with the SqlDataSource Control.

See Also

Other Resources

Data Source Web Server Controls