Redigeeri

Jagamisviis:


SqlDataSource.EnableCaching Property

Definition

Gets or sets a value indicating whether the SqlDataSource control has data caching enabled.

public:
 virtual property bool EnableCaching { bool get(); void set(bool value); };
public virtual bool EnableCaching { get; set; }
member this.EnableCaching : bool with get, set
Public Overridable Property EnableCaching As Boolean

Property Value

true if data caching is enabled for the data source control; otherwise, false. The default is false.

Exceptions

The EnableCaching property is set to true when caching is not supported by the SqlDataSource.

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. The EnableCaching property is set to true and the CacheDuration is set to 20 seconds. Because the CacheExpirationPolicy property is set to the Absolute field 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>
<%@ 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>

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 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 retrieving a fresh set.

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