SqlDataSource.EnableCaching SqlDataSource.EnableCaching SqlDataSource.EnableCaching SqlDataSource.EnableCaching Property

定义

获取或设置一个值,该值指示 SqlDataSource 控件是否启用数据缓存。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

属性值

如果为数据源控件启用数据缓存,则为 true;否则为 falsetrue if data caching is enabled for the data source control; otherwise, false. 默认值为 falseThe default is false.

异常

EnableCaching 不支持缓存时,SqlDataSource 属性设置为 trueThe EnableCaching property is set to true when caching is not supported by the SqlDataSource.

示例

下面的代码示例演示了如何在 Microsoft SQL Server 中从 Northwind 数据库检索数据, 并在GridView控件中显示数据缓存。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. 属性设置为true , 并且CacheDuration设置为20秒。 EnableCachingThe EnableCaching property is set to true and the CacheDuration is set to 20 seconds. 由于默认CacheExpirationPolicy情况下, 此属性Absolute设置为字段, 此页的用户每隔20秒就会看到最新数据。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>

注解

SqlDataSource控件支持数据缓存。The SqlDataSource control supports data caching. 在缓存数据时, Select方法从缓存而不是基础数据库中检索数据。While data is cached, the Select method retrieves data from the cache rather than from the underlying database. 缓存过期后, 该方法Select将从基础数据库中检索数据, 然后再次缓存数据。When the cache expires, the Select method retrieves data from the underlying database, and then caches the data again.

当属性设置为true , 并且CacheDuration属性设置为大于0的值时,控件将自动缓存数据,这表示缓存在检索之前存储数据的秒数SqlDataSource EnableCaching全新集。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.

重要

使用 Microsoft Windows 身份验证下的客户端模拟时, 将在第一个用户访问数据时缓存数据。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.

适用于

另请参阅