加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

在ASP.NET 2.0中操作数据之五十九:使用SQL缓存依赖项SqlCacheDe

发布时间:2016-11-24 04:41:53 所属栏目:MsSql教程 来源:站长网
导读:导言: 在56和57章探讨的缓存技术使用的是基于时间的缓存周期,当过了某段时间后便将缓存数据从内存清除。当设置缓存时间为x秒时,数据在x秒内都是“新”的。当然,就像在60章谈到的那样,对静态数据来说,x可延伸到web应用程序的整个生命周期(lifetime)。

  在《使用ObjectDataSource缓存数据》教程里,我们考察了声明ObjectDataSource控件的缓存功能。仅仅将EnableCaching属性设置为true,并将acheDuration属性设置为某个时间间(timeinterval),ObjectDataSource控件就会自动地将从“源对象”(underlying object)返回的数据进行缓存。ObjectDataSource控件可以使用单个或多个SQL cache dependencies.

  为此,打开文件夹Caching里的SqlCacheDependencies.aspx页面,在设计模式里,从工具箱拖一个GridView控件到页面上,设置其ID为ProductsDeclarative ,从其智能标签里将其绑定到一个名为ProductsDataSourceDeclarative的ObjectDataSource.

/uploads/allimg/c161121/14OI94T043P-124626.gif
图5:创建一个名为ProductsDataSourceDeclarative的ObjectDataSource

  设置该ObjectDataSource使用ProductsBLL类。在SELECT标签里选GetProducts()方法;在UPDATE标签里,选择包含3个输入参数——productName,unitPrice,和productID的UpdateProduct重载方法;在INSERT 和 DELETE标签里选“(None)”.

/uploads/allimg/c161121/14OI94ZE940-135a1.gif
图6:使用包含3个输入参数的UpdateProduct重载方法

/uploads/allimg/c161121/14OI9491050Z-143610.gif
图7:在INSERT和DELETE标签的下拉列表里选“(None)”

  完成设置后,Visual Studio会为GridView里的每一列创建绑定列(BoundFields) 和CheckBoxFieldsL列。将ProductName, CategoryName, 和UnitPrice以外的列都删除,对其应用什么格式化都可以。在GridView的智能标签里启用分页、排序、编辑功能。Visual Studio会将ObjectDataSource控件的OldValuesParameterFormatString属性设置为original_{0},为使GridView的编辑功能运行正常,要么删除该属性,要么将其设置为默认值:{0}.

  最后,在GridView上面添加一个Label Web控件,设置其ID为ODSEvents,再将其EnableViewState属性设置为false.做完上述修改后,页面的声明代码看起来应该和下面的差不多。注意,我已经对GridView列的外观做了些定制,虽然这对SQL cache dependency功能来说并不是必要的。

<asp:Label ID="ODSEvents" runat="server" EnableViewState="False" />

<asp:GridView ID="ProductsDeclarative" runat="server"
 AutoGenerateColumns="False" DataKeyNames="ProductID"
 DataSourceID="ProductsDataSourceDeclarative"
 AllowPaging="True" AllowSorting="True">
 <Columns>
 <asp:CommandField ShowEditButton="True" />
 <asp:TemplateField HeaderText="Product" SortExpression="ProductName">
  <EditItemTemplate>
  <asp:TextBox ID="ProductName" runat="server"
   Text='<%# Bind("ProductName") %>' />
  <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
   ControlToValidate="ProductName" Display="Dynamic"
   ErrorMessage="You must provide a name for the product."
   SetFocusOnError="True"
   runat="server">*</asp:RequiredFieldValidator>
  </EditItemTemplate>
  <ItemTemplate>
  <asp:Label ID="Label2" runat="server"
   Text='<%# Bind("ProductName") %>' />
  </ItemTemplate>
 </asp:TemplateField>
 <asp:BoundField DataField="CategoryName" HeaderText="Category"
  ReadOnly="True" SortExpression="CategoryName" />
 <asp:TemplateField HeaderText="Price" SortExpression="UnitPrice">
  <EditItemTemplate>
  $<asp:TextBox ID="UnitPrice" runat="server" Columns="8"
   Text='<%# Bind("UnitPrice", "{0:N2}") %>'></asp:TextBox>
  <asp:CompareValidator ID="CompareValidator1" runat="server"
   ControlToValidate="UnitPrice"
   ErrorMessage="You must enter a valid currency value with
   no currency symbols. Also, the value must be greater than
   or equal to zero."
   Operator="GreaterThanEqual" SetFocusOnError="True"
   Type="Currency" Display="Dynamic"
   ValueToCompare="0">*</asp:CompareValidator>
  </EditItemTemplate>
  <ItemStyle HorizontalAlign="Right" />
  <ItemTemplate>
  <asp:Label ID="Label1" runat="server"
   Text='<%# Bind("UnitPrice", "{0:c}") %>' />
  </ItemTemplate>
 </asp:TemplateField>
 </Columns>
</asp:GridView>

<asp:ObjectDataSource ID="ProductsDataSourceDeclarative" runat="server"
 SelectMethod="GetProducts" TypeName="ProductsBLL"
 UpdateMethod="UpdateProduct">
 <UpdateParameters>
 <asp:Parameter Name="productName" Type="String" />
 <asp:Parameter Name="unitPrice" Type="Decimal" />
 <asp:Parameter Name="productID" Type="Int32" />
 </UpdateParameters>
</asp:ObjectDataSource>

下一步,为ObjectDataSource控件的Selecting事件创建一个事件处理器:

protected void ProductsDataSourceDeclarative_Selecting
 (object sender, ObjectDataSourceSelectingEventArgs e)
{
 ODSEvents.Text = "-- Selecting event fired";
}

  我们知道,只有当ObjectDataSource控件从它的相关“源对象”(underlying object)获取数据时才会触发它的Selecting事件。如果ObjectDataSource是从内存检索数据的话,将不会触发Selecting事件.

  现在,在浏览器里登录该页面,因为我们还没有进行缓存,所以每当你分页、排序、编辑时,页面都会显示文本——“—Selecting event fired”, 如图8所示:

(编辑:源码网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读