要利用asp.net2.0中的新SQL Server高速缓存依赖性,必须SQL Server数据库支持该功能.可使用C:/Windows/Microsoft .NET/Framework/V2.0XXXXX/下的aspnet_regsql.exe工具.
1.使数据库支持
2.使表支持SQL高速缓存依赖性
另外
3.在<configuration>节点下配置数据库链接字符串
4.在<system.web>节点下配置
5.客户端代码
6.服务端代码
1.使数据库支持
aspnet_regsql.exe
-
S 服务器地址 -U 数据库用户名
-
P 数据库密码
-
d 数据库名称
-
dd
2.使表支持SQL高速缓存依赖性
aspnet_regsql.exe
-S 服务器地址 -U 数据库用户名
-
P 数据库密码
-
d 数据库名称
-
t 表名
-
et
另外
--
使数据库不支持SQL高速缓存依赖性
aspnet_regsql.exe -S 服务器地址 -U 数据库用户名 - P 数据库密码 - d 数据库名称 - ed
-- 使表不支持SQL高速缓存依赖性
aspnet_regsql.exe -S 服务器地址 -U 数据库用户名 - P 数据库密码 - d 数据库名称 - t 表名 - dt
-- 查询当前数据库支持SQL高速缓存依赖性的表
aspnet_regsql.exe -S 服务器地址 -U 数据库用户名 - P 数据库密码 - d 数据库名称 - lt
aspnet_regsql.exe -S 服务器地址 -U 数据库用户名 - P 数据库密码 - d 数据库名称 - ed
-- 使表不支持SQL高速缓存依赖性
aspnet_regsql.exe -S 服务器地址 -U 数据库用户名 - P 数据库密码 - d 数据库名称 - t 表名 - dt
-- 查询当前数据库支持SQL高速缓存依赖性的表
aspnet_regsql.exe -S 服务器地址 -U 数据库用户名 - P 数据库密码 - d 数据库名称 - lt
3.在<configuration>节点下配置数据库链接字符串
<
connectionStrings
><
add name
=
"
ConnectionString
"
connectionString
=
"
server=服务器;uid=用户名;pwd=密码;database=数据库名称
"
/></
connectionStrings
>
4.在<system.web>节点下配置
<
caching
>
< sqlCacheDependency enabled = " true " pollTime = " 10000 " >
< databases >
< add name = " 数据库名称 " connectionStringName = " ConnectionString " pollTime = " 缓存时间,以秒为单位 " /><!-- ConnectionString是变量名称,即上面的数据库链接字符串 -->
</ databases >
</ sqlCacheDependency >
</ caching >
< sqlCacheDependency enabled = " true " pollTime = " 10000 " >
< databases >
< add name = " 数据库名称 " connectionStringName = " ConnectionString " pollTime = " 缓存时间,以秒为单位 " /><!-- ConnectionString是变量名称,即上面的数据库链接字符串 -->
</ databases >
</ sqlCacheDependency >
</ caching >
5.客户端代码
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default3.aspx.cs
"
Inherits
=
"
Default3
"
%>
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
< title > 无标题页 </ title >
</ head >
< body >
< form id = " form1 " runat = " server " >
< asp:Label ID = " Label1 " runat = " server " Text = " Label " ></ asp:Label >
< asp:GridView ID = " GridView1 " runat = " server " >
</ asp:GridView >
</ form >
</ body >
</ html >
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
< title > 无标题页 </ title >
</ head >
< body >
< form id = " form1 " runat = " server " >
< asp:Label ID = " Label1 " runat = " server " Text = " Label " ></ asp:Label >
< asp:GridView ID = " GridView1 " runat = " server " >
</ asp:GridView >
</ form >
</ body >
</ html >
6.服务端代码
using
System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
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;
using System.Web.Caching;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
DataSet dt;
dt = (DataSet)Cache[ " firmOrders " ];
if (dt == null )
{
string connStr = ConfigurationManager.ConnectionStrings[ " ConnectionString " ].ConnectionString;
string sql = " select * from Orders " ;
SqlConnection conn = new SqlConnection(connStr);
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
dt = new DataSet();
da.Fill(dt);
SqlCacheDependency myDependency = new SqlCacheDependency( " Northwind " , " Orders " );
Cache.Insert( " firmOrders " , dt, myDependency);
Label1.Text = " 从数据库读取 " ;
}
else
{
Label1.Text = " 从内存读取 " ;
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
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;
using System.Web.Caching;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
DataSet dt;
dt = (DataSet)Cache[ " firmOrders " ];
if (dt == null )
{
string connStr = ConfigurationManager.ConnectionStrings[ " ConnectionString " ].ConnectionString;
string sql = " select * from Orders " ;
SqlConnection conn = new SqlConnection(connStr);
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
dt = new DataSet();
da.Fill(dt);
SqlCacheDependency myDependency = new SqlCacheDependency( " Northwind " , " Orders " );
Cache.Insert( " firmOrders " , dt, myDependency);
Label1.Text = " 从数据库读取 " ;
}
else
{
Label1.Text = " 从内存读取 " ;
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}