ObjectDataSource.EnableCaching 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置一个值,该值指示 ObjectDataSource 控件是否启用数据缓存。
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
;否则为 false
。 默认值为 false
。
例外
当由 EnableCaching 属性指定的方法返回 SelectMethod 时,会将 DbDataReader 属性设置为 true
。
示例
以下三个示例演示一个网页、一个代码隐藏页类和一个从 Northwind 数据库中的 Employees 表检索记录的数据访问类。
第一个示例演示一个网页,其中包含两 ObjectDataSource 个控件、一个 DropDownList 控件和一个 DetailsView 控件。 第一个 ObjectDataSource 控件和 DropDownList 控件用于检索和显示数据库中的员工姓名。 第二 ObjectDataSource 个控件和 DetailsView 控件用于检索和显示用户选择的员工记录。
为 ObjectDataSource 控件启用了缓存。 因此,每条记录仅从数据库检索一次。 属性 CacheKeyDependency 设置为“EmployeeDetails”,但任何字符串值都可以用作键。 该网页还包括一个 Button 控件,用户可以单击该控件使缓存的数据过期。
<form id="form1" runat="server">
<div>
<asp:objectdatasource
ID="ObjectDataSource1"
runat="server"
SelectMethod="GetFullNamesAndIDs"
TypeName="Samples.AspNet.CS.EmployeeLogic" />
<p>
<asp:dropdownlist
ID="DropDownList1"
runat="server"
DataSourceID="ObjectDataSource1"
DataTextField="FullName"
DataValueField="EmployeeID"
AutoPostBack="True"
AppendDataBoundItems="true">
<asp:ListItem Text="Select One" Value=""></asp:ListItem>
</asp:dropdownlist>
</p>
<asp:objectdatasource
ID="ObjectDataSource2"
runat="server"
SelectMethod="GetEmployee"
TypeName="Samples.AspNet.CS.EmployeeLogic"
EnableCaching="true"
CacheKeyDependency="EmployeeDetails" >
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" DefaultValue="-1" Name="empID" />
</SelectParameters>
</asp:objectdatasource>
<asp:DetailsView
ID="DetailsView1"
runat="server"
DataSourceID="ObjectDataSource2"
AutoGenerateRows="false">
<Fields>
<asp:BoundField HeaderText="Address" DataField="Address" />
<asp:BoundField HeaderText="City" DataField="City" />
<asp:BoundField HeaderText="Postal Code" DataField="PostalCode" />
</Fields>
</asp:DetailsView>
<asp:Button
ID="Button1"
runat="server"
Text="Check for latest data"
OnClick="Button1_Click" />
</div>
</form>
<form id="form1" runat="server">
<div>
<asp:objectdatasource
ID="ObjectDataSource1"
runat="server"
SelectMethod="GetFullNamesAndIDs"
TypeName="Samples.AspNet.CS.EmployeeLogic" />
<p>
<asp:dropdownlist
ID="DropDownList1"
runat="server"
DataSourceID="ObjectDataSource1"
DataTextField="FullName"
DataValueField="EmployeeID"
AutoPostBack="True"
AppendDataBoundItems="true">
<asp:ListItem Text="Select One" Value=""></asp:ListItem>
</asp:dropdownlist>
</p>
<asp:objectdatasource
ID="ObjectDataSource2"
runat="server"
SelectMethod="GetEmployee"
TypeName="Samples.AspNet.CS.EmployeeLogic"
EnableCaching="true"
CacheKeyDependency="EmployeeDetails" >
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" DefaultValue="-1" Name="empID" />
</SelectParameters>
</asp:objectdatasource>
<asp:DetailsView
ID="DetailsView1"
runat="server"
DataSourceID="ObjectDataSource2"
AutoGenerateRows="false">
<Fields>
<asp:BoundField HeaderText="Address" DataField="Address" />
<asp:BoundField HeaderText="City" DataField="City" />
<asp:BoundField HeaderText="Postal Code" DataField="PostalCode" />
</Fields>
</asp:DetailsView>
<asp:Button
ID="Button1"
runat="server"
Text="Check for latest data"
OnClick="Button1_Click" />
</div>
</form>
第二个示例显示了 事件的处理程序Load和 控件的 Button 事件的处理程序Click。 事件处理程序 Load 创建一个缓存项,其中键设置为 CacheKeyDependency 值。 事件处理程序 Click 删除键等于 CacheKeyDependency 值的缓存项。 删除缓存项后,依赖于密钥的所有缓存数据都将过期。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Cache[ObjectDataSource2.CacheKeyDependency] = "CacheExample";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Cache.Remove(ObjectDataSource2.CacheKeyDependency);
Cache[ObjectDataSource2.CacheKeyDependency] = "CacheExample";
DetailsView1.DataBind();
}
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not (IsPostBack) Then
Cache(ObjectDataSource2.CacheKeyDependency) = "CacheExample"
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Cache.Remove(ObjectDataSource2.CacheKeyDependency)
Cache(ObjectDataSource2.CacheKeyDependency) = "CacheExample"
DetailsView1.DataBind()
End Sub
第三个示例演示与 Northwind 数据库交互的数据访问类。 类使用 LINQ 查询 Employees 表。 该示例需要一个 LINQ to SQL 类,该类表示 Northwind 数据库和 Employees 表。 有关详细信息,请参阅 如何:在 Web 项目中创建 LINQ to SQL 类。
public class EmployeeLogic
{
public static Array GetFullNamesAndIDs()
{
NorthwindDataContext ndc = new NorthwindDataContext();
var employeeQuery =
from e in ndc.Employees
orderby e.LastName
select new { FullName = e.FirstName + " " + e.LastName, EmployeeID = e.EmployeeID };
return employeeQuery.ToArray();
}
public static Employee GetEmployee(int empID)
{
if (empID < 0)
{
return null;
}
else
{
NorthwindDataContext ndc = new NorthwindDataContext();
var employeeQuery =
from e in ndc.Employees
where e.EmployeeID == empID
select e;
return employeeQuery.Single();
}
}
public static void UpdateEmployeeAddress(Employee originalEmployee, string address, string city, string postalcode)
{
NorthwindDataContext ndc = new NorthwindDataContext();
ndc.Employees.Attach(originalEmployee, false);
originalEmployee.Address = address;
originalEmployee.City = city;
originalEmployee.PostalCode = postalcode;
ndc.SubmitChanges();
}
}
Public Class EmployeeLogic
Public Shared Function GetFullNamesAndIDs() As Array
Dim ndc As New NorthwindDataContext()
Dim employeeQuery = _
From e In ndc.Employees _
Order By e.LastName _
Select FullName = e.FirstName + " " + e.LastName, EmployeeID = e.EmployeeID
Return employeeQuery.ToArray()
End Function
Public Shared Function GetEmployee(ByVal empID As Integer) As Employee
If (empID < 0) Then
Return Nothing
Else
Dim ndc As New NorthwindDataContext()
Dim employeeQuery = _
From e In ndc.Employees _
Where e.EmployeeID = empID _
Select e
Return employeeQuery.Single()
End If
End Function
Public Shared Sub UpdateEmployeeAddress(ByVal originalEmployee As Employee, ByVal address As String, ByVal city As String, ByVal postalcode As String)
Dim ndc As New NorthwindDataContext()
ndc.Employees.Attach(originalEmployee, False)
originalEmployee.Address = address
originalEmployee.City = city
originalEmployee.PostalCode = postalcode
ndc.SubmitChanges()
End Sub
End Class
注解
控件 ObjectDataSource 支持数据缓存。 缓存数据时,调用 Select 方法从缓存中检索数据, ObjectDataSource 而不是创建业务对象的实例并调用其数据方法。 缓存过期时, Select 方法从业务对象中检索数据,然后再次缓存数据。
当 属性设置为 true
且 CacheDuration 属性设置为大于 0 的值时EnableCaching,控件ObjectDataSource会自动缓存数据,该值指示缓存在丢弃缓存项之前存储数据的秒数。 值为 0 表示缓存无限长。