要利用asp.net2.0中的新SQL Server高速缓存依赖性,必须SQL Server数据库支持该功能.可使用C:\Windows\Microsoft .NET\Framework\V2.0XXXXX\下的aspnet_regsql.exe工具或者使用SqlCacheDependencyAdmin类编写代码实现
1.使用aspnet_regsql.exe工具
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
2.使用SqlCacheDependencyAdmin类编写代码实现
string
connStr
=
ConfigurationManager.ConnectionStrings[
"
ConnectionString
"
].ConnectionString;
SqlCacheDependencyAdmin.EnableNotifications(connStr); // 使数据库支持SQL高速缓存依赖性
SqlCacheDependencyAdmin.DisableNotifications(connStr); // 使数据库禁用SQL高速缓存依赖性
SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, tableName); // 使表支持SQL高速缓存依赖性,tableName为表名
SqlCacheDependencyAdmin.DisableTableForNotifications(connStr, tableName); // 使表禁止SQL高速缓存依赖性
SqlCacheDependencyAdmin.EnableNotifications(connStr); // 使数据库支持SQL高速缓存依赖性
SqlCacheDependencyAdmin.DisableNotifications(connStr); // 使数据库禁用SQL高速缓存依赖性
SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, tableName); // 使表支持SQL高速缓存依赖性,tableName为表名
SqlCacheDependencyAdmin.DisableTableForNotifications(connStr, tableName); // 使表禁止SQL高速缓存依赖性
3.在<configuration>节点下配置数据库链接字符串
来源:(http://blog.sina.com.cn/s/blog_4ae178ba0100dugg.html) - .net高速缓存(鉴)_南冠彤_新浪博客
<
connectionStrings
><
add name
=
"
ConnectionString
"
connectionString
=
"
server=服务器;uid=用户名;pwd=密码;database=数据库名称
"
/></
connectionStrings
>
4.在<system.web>节点下配置
<
caching
>
< sqlCacheDependency enabled = " true " pollTime = " 10000 " ><!--enabled指示是否轮询更改,pollTime缓存时间,以秒为单位-->
< databases >
< add name = "cacheName " connectionStringName = " ConnectionString " /><!-- connectionStringName指定在<connectionStrings>中添加的连接字符串。name则是为该SqlCacheDependency起的名字,这个名字将在第6步中用到。 -->
</ databases >
</ sqlCacheDependency >
</ caching >
< sqlCacheDependency enabled = " true " pollTime = " 10000 " ><!--enabled指示是否轮询更改,pollTime缓存时间,以秒为单位-->
< databases >
< add name = "cacheName " connectionStringName = " ConnectionString " /><!-- connectionStringName指定在<connectionStrings>中添加的连接字符串。name则是为该SqlCacheDependency起的名字,这个名字将在第6步中用到。 -->
</ databases >
</ sqlCacheDependency >
</ caching >
5.客户端代码
< CodeFile="Default3.aspx.cs" Inherits="Default3"%">%@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( " cacheName " , " 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( " cacheName " , " Orders " );
Cache.Insert( " firmOrders " , dt, myDependency);
Label1.Text = " 从数据库读取 " ;
}
else
{
Label1.Text = " 从内存读取 " ;
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
来源:(http://blog.sina.com.cn/s/blog_4ae178ba0100dugg.html) - .net高速缓存(鉴)_南冠彤_新浪博客