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 個控制項、一個控制項和一個 DropDownListDetailsView 控制項的網頁。 第一個 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 處理常式 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 方法會從商務物件擷取資料,然後再次快取資料。

當 屬性設定為 trueCacheDuration 屬性設定為大於 0 的值時 EnableCaching ,控制項 ObjectDataSource 會自動快取資料,這表示快取在捨棄快取專案之前儲存資料的秒數。 值為 0 表示無限長快取。

適用於

另請參閱