共用方式為


ObjectDataSource.EnableCaching 屬性

定義

取得或設定值,指出 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 控件是用來從資料庫擷取和顯示員工名稱。 第二 ObjectDataSourceDetailsView 控件和控件是用來擷取及顯示使用者所選取的員工記錄。

控件已啟用 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處理程式ClickLoad事件處理程式會建立快取專案,並將索引鍵設定為 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 資料表。 此範例需要代表 Northwind 資料庫和 Employees 數據表的 LINQ to SQL 類別。 如需詳細資訊,請參閱 如何:在 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 方法會從商務物件擷取數據,然後再次快取數據。

ObjectDataSource 屬性設定true為 且 CacheDuration 屬性設定為大於 0 的值時EnableCaching,控件會自動快取數據,這表示快取在捨棄快取專案之前儲存數據的秒數。 值為 0 表示無限長快取。

適用於

另請參閱