目前大多数数据库应用程序都是基于Sql2000的,所以我们着重讲述在2000怎样实现缓存,然后利用ASP.NETAJAX实现无刷新。
使用轮询的Sql数据缓存依赖功能,必须正确实现以下三个步骤:
一:配置Sql数据库,以启用Sql数据缓存依赖支持;
二:配置Web.config文件;
三:实现具体应用程序,使用sql数据缓存依赖功能。
然后再利用ASP。NETAJAX实现无刷新功能。
在VS2005中打开SDK命令窗口,首先设置要缓存的库(可以利用aspnet_regsql -? 查看帮助)
“aspnet_regsql –S localhost –U sa –P sa –d northwind -ed”
然后设置要缓存的表
“aspnet_regsql –S localhost –U sa –P sa –d northwind –t Customers -td”
这样我们就设置了Customers这张表为缓存表
如果想禁用数据库和表的话:把后面的参数改为“-dd”,“-dt”
这样第一步我们就设置好了,下一步
在WEB。CONFIG中我们设置缓存
<caching>
<sqlCacheDependency enabled="true" pollTime="600">
<databases>
<add name="Northwind" connectionStringName="NorthwindConnectionString"/>
</databases>
</sqlCacheDependency>
</caching>
其中“NorthwindConnectionString”为数据库连接字符串
“Northwind”为数据库名称
第三步:实现缓存,它包括好几种方法,我们利用ObjectDataSource方法程序如下:
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="10000">
</asp:Timer>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID"
DataSourceID="ObjectDataSource1" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" EnableViewState="False" Font-Size="9pt" Width="649px">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:CommandField ButtonType="Button" ShowEditButton="True" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete" EnableCaching="True" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" SqlCacheDependency="Northwind:Customers" TypeName="NortwindDbTableAdapters.CustomersTableAdapter" UpdateMethod="UpdateQuery">
<DeleteParameters>
<asp:Parameter Name="Original_CustomerID" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="CustomerID" Type="String" />
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="ContactName" Type="String" />
<asp:Parameter Name="Address" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="CustomerID" Type="String" />
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="ContactName" Type="String" />
<asp:Parameter Name="Address" Type="String" />
</InsertParameters>
</asp:ObjectDataSource>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
其中包括了ASP.NETAJAX
浙公网安备 33010602011771号