要利用asp.net2.0中的新SQL Server高速缓存依赖性,必须SQL Server数据库支持该功能.可使用C:\Windows\Microsoft .NET\Framework\V2.0XXXXX\下的aspnet_regsql.exe工具或者使用SqlCacheDependencyAdmin类编写代码实现
<wbr></wbr>
1.使用aspnet_regsql.exe工具
1).使数据库支持
aspnet_regsql.exe<wbr></wbr>
-
S<wbr>服务器地址 -U 数据库用户名<wbr></wbr></wbr>
-
P<wbr>数据库密码<wbr></wbr></wbr>
-
d<wbr>数据库名称<wbr></wbr></wbr>
-
dd
2).使表支持SQL高速缓存依赖性
aspnet_regsql.exe<wbr></wbr>
-S<wbr>服务器地址 -U 数据库用户名</wbr>
<wbr></wbr>
-
P<wbr>数据库密码<wbr></wbr></wbr>
-
d<wbr>数据库名称<wbr></wbr></wbr>
-
t<wbr>表名<wbr></wbr></wbr>
-
et
另外
--
使数据库不支持SQL高速缓存依赖性
aspnet_regsql.exe<wbr></wbr>
-S<wbr>服务器地址 -U 数据库用户名</wbr>
<wbr></wbr>
-
P<wbr>数据库密码<wbr></wbr></wbr>
-
d<wbr>数据库名称<wbr></wbr></wbr>
-
ed
--
使表不支持SQL高速缓存依赖性
aspnet_regsql.exe<wbr></wbr>
-S<wbr>服务器地址 -U 数据库用户名</wbr>
<wbr></wbr>
-
P<wbr>数据库密码<wbr></wbr></wbr>
-
d<wbr>数据库名称<wbr></wbr></wbr>
-
t<wbr>表名<wbr></wbr></wbr>
-
dt
--
查询当前数据库支持SQL高速缓存依赖性的表
aspnet_regsql.exe<wbr></wbr>
-S<wbr>服务器地址 -U 数据库用户名</wbr>
<wbr></wbr>
-
P<wbr>数据库密码<wbr></wbr></wbr>
-
d<wbr>数据库名称<wbr></wbr></wbr>
-
lt
<wbr></wbr>
2.使用SqlCacheDependencyAdmin类编写代码实现
<wbr></wbr>
string
<wbr>connStr<wbr></wbr></wbr>
=
<wbr>ConfigurationManager.ConnectionStrings[</wbr>
"
ConnectionString
"
].ConnectionString;
SqlCacheDependencyAdmin.EnableNotifications(connStr);
//
使数据库支持SQL高速缓存依赖性
SqlCacheDependencyAdmin.DisableNotifications(connStr);
//
使数据库禁用SQL高速缓存依赖性
SqlCacheDependencyAdmin.EnableTableForNotificati<wbr>ons(connStr,<wbr>tableName);</wbr></wbr>
//
使表支持SQL高速缓存依赖性,tableName为表名
SqlCacheDependencyAdmin.DisableTableForNotificat<wbr>ions(connStr,<wbr>tableName);</wbr></wbr>
//
使表禁止SQL高速缓存依赖性
3.在<configuration>节点下配置数据库链接字符串
<
connectionStrings
><
add<wbr>name</wbr>
=
"
ConnectionString
"
<wbr>connectionString</wbr>
=
"
server=服务器;uid=用户名;pwd=密码;database=数据库名称
"
/></
connectionStrings
>
4.在<system.web>节点下配置
<
caching
>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<
sqlCacheDependency<wbr>enabled</wbr>
=
"
true
"
<wbr>pollTime</wbr>
=
"
10000
"
><!--enabled指示是否轮询更改,pollTime缓存时间,以秒为单位-->
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<
databases
>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<
add<wbr>name</wbr>
=
"cacheName
"
<wbr>connectionStringName</wbr>
=
"
ConnectionString
"
/><!--
connectionStringName指定在<connectionStrings>中添加的连接字符串。name则是为该SqlCacheDependency起的名字,这个名字将在第6步中用到。
-->
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
</
databases
>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
</
sqlCacheDependency
>
<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
</
caching
>
5.客户端代码
<<a href="mailto:%@Page%C2%A0<wbr>Language=" c#"<wbr style="text-decoration:none; color:rgb(8,68,135)"><wbr>CodeFile="Default3.aspx.cs"<wbr>Inherits="Default3"%">%<span style="word-wrap:normal; word-break:normal; color:rgb(0,0,0)">@Page<wbr>Language</wbr></span><span style="word-wrap:normal; word-break:normal; color:rgb(0,0,0)">=</span><span style="word-wrap:normal; word-break:normal; color:rgb(128,0,0)">"</span><span style="word-wrap:normal; word-break:normal; color:rgb(128,0,0)">C#</span><span style="word-wrap:normal; word-break:normal; color:rgb(128,0,0)">"</span><span style="word-wrap:normal; word-break:normal; color:rgb(0,0,0)"><wbr>AutoEventWireup</wbr></span><span style="word-wrap:normal; word-break:normal; color:rgb(0,0,0)">=</span><span style="word-wrap:normal; word-break:normal; color:rgb(128,0,0)">"</span><span style="word-wrap:normal; word-break:normal; color:rgb(128,0,0)">true</span><span style="word-wrap:normal; word-break:normal; color:rgb(128,0,0)">"</span><span style="word-wrap:normal; word-break:normal; color:rgb(0,0,0)"><wbr>CodeFile</wbr></span><span style="word-wrap:normal; word-break:normal; color:rgb(0,0,0)">=</span><span style="word-wrap:normal; word-break:normal; color:rgb(128,0,0)">"</span><span style="word-wrap:normal; word-break:normal; color:rgb(128,0,0)">Default3.aspx.cs</span><span style="word-wrap:normal; word-break:normal; color:rgb(128,0,0)">"</span><span style="word-wrap:normal; word-break:normal; color:rgb(0,0,0)"><wbr>Inherits</wbr></span><span style="word-wrap:normal; word-break:normal; color:rgb(0,0,0)">=</span><span style="word-wrap:normal; word-break:normal; color:rgb(128,0,0)">"</span><span style="word-wrap:normal; word-break:normal; color:rgb(128,0,0)">Default3</span><span style="word-wrap:normal; word-break:normal; color:rgb(128,0,0)">"</span><span style="word-wrap:normal; word-break:normal; color:rgb(0,0,0)">%</span>></wbr></wbr></wbr>
<
!DOCTYPE<wbr>html<wbr>PUBLIC</wbr></wbr>
"
//W3C//DTD<wbr>XHTML<wbr>1.0<wbr>Transitional//EN</wbr></wbr></wbr>
"
<wbr></wbr>
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
<
html<wbr>xmlns</wbr>
=
"
http://www.w3.org/1999/xhtml
"
<wbr></wbr>
>
<
head<wbr>runat</wbr>
=
"
server
"
>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<
title
>
无标题页
</
title
>
</
head
>
<
body
>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<
form<wbr>id</wbr>
=
"
form1
"
<wbr>runat</wbr>
=
"
server
"
>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<
asp:Label<wbr>ID</wbr>
=
"
Label1
"
<wbr>runat</wbr>
=
"
server
"
<wbr>Text</wbr>
=
"
Label
"
></
asp:Label
>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<
asp:GridView<wbr>ID</wbr>
=
"
GridView1
"
<wbr>runat</wbr>
=
"
server
"
>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
</
asp:GridView
>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
</
form
>
</
body
>
</
html
>
6.服务端代码
using
<wbr>System;<br></wbr>
using
<wbr>System.Data;<br></wbr>
using
<wbr>System.Data.SqlClient;<br></wbr>
using
<wbr>System.Configuration;<br></wbr>
using
<wbr>System.Collections;<br></wbr>
using
<wbr>System.Web;<br></wbr>
using
<wbr>System.Web.Security;<br></wbr>
using
<wbr>System.Web.UI;<br></wbr>
using
<wbr>System.Web.UI.WebControls;<br></wbr>
using
<wbr>System.Web.UI.WebControls.WebParts;<br></wbr>
using
<wbr>System.Web.UI.HtmlControls;<br><br></wbr>
using
<wbr>System.Web.Caching;</wbr>
public
<wbr></wbr>
partial
<wbr></wbr>
class
<wbr>Default3<wbr>:<wbr>System.Web.UI.Page<br> {<br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
protected
<wbr></wbr>
void
<wbr>Page_Load(</wbr>
object
<wbr>sender,<wbr>EventArgs<wbr>e)<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>DataSet<wbr>dt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>dt<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
=
<wbr>(DataSet)Cache[</wbr>
"
firmOrders
"
];
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
if
<wbr>(dt<wbr></wbr></wbr>
==
<wbr></wbr>
null
)
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
string
<wbr>connStr<wbr></wbr></wbr>
=
<wbr>ConfigurationManager.ConnectionStrings[</wbr>
"
ConnectionString
"
].ConnectionString;
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
string
<wbr>sql<wbr></wbr></wbr>
=
<wbr></wbr>
"
select<wbr>*<wbr>from<wbr>Orders</wbr></wbr></wbr>
"
;
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>SqlConnection<wbr>conn<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
=
<wbr></wbr>
new
<wbr>SqlConnection(connStr);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>SqlDataAdapter<wbr>da<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
=
<wbr></wbr>
new
<wbr>SqlDataAdapter(sql,<wbr>conn);<br><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>dt<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
=
<wbr></wbr>
new
<wbr>DataSet();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>da.Fill(dt);<br><span style="font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(186,186,186); word-wrap:normal; word-break:normal">//不加下面两句会报错</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
SqlCacheDependencyAdmin.EnableNotifications(connStr);//使数据库支持SQL高速缓存依赖性
SqlCacheDependencyAdmin.EnableTableForNotificati<wbr>ons(connStr,<wbr>tableName);</wbr></wbr>//使表支持
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>SqlCacheDependency<wbr>myDependency<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
=
<wbr></wbr>
new
<wbr>SqlCacheDependency(</wbr>
"
cacheName
"
,<wbr></wbr>
"
Orders
"
);
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Cache.Insert(</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
"
firmOrders
"
,<wbr>dt,<wbr>myDependency);<br><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Label1.Text<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
=
<wbr></wbr>
"
从数据库读取
"
;
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
else
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Label1.Text<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
=
<wbr></wbr>
"
从内存读取
"
;
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>GridView1.DataSource<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
=
<wbr>dt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>GridView1.DataBind();<br><wbr><wbr><wbr><wbr>}<br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>