Asp.net 2.0 提供了一个新的数据缓存功能,就是利用sql server2005 的异步通知功能来实现缓存
1.首先在sqlserver2005 中创建一个test的数据库.添加一个 employee的数据库表.
1
CREATE TABLE [dbo].[employee](
2
[id] [int] IDENTITY(1,1) NOT NULL,
3
[name] [varchar](50)
4
)
5![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2使用 vs2005 创建一个新的asp.net项目.
web.config如下
1
<?xml version="1.0"?>
2
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
3
<appSettings/>
4
<connectionStrings>
5
<add name="mySource" connectionString="Data Source=.\sql2005;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=sasa" providerName="System.Data.SqlClient"></add>
6
</connectionStrings>
7
<system.web>
8
<compilation debug="true"/>
9
<authentication mode="Windows"/>
10
</system.web>
11
</configuration>
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3.编写global.asax文件,启动监听sql2005通知事件.
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Application_Start(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string connStr=ConfigurationManager.ConnectionStrings["mySource"].ConnectionString;
SqlDependency.Start(connStr);
}
void Application_End(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string connStr = ConfigurationManager.ConnectionStrings["mySource"].ConnectionString;
SqlDependency.Stop(connStr);
}
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4.编写数据访问代码.创建一个EmployeeData的类,代码如下
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.Common;
using System.Web;
using System.Web.Caching;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// EmployeeData 的摘要说明
/// </summary>
public class EmployeeData
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public EmployeeData()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private HttpContext context;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DataSet GetCacheData()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
context = HttpContext.Current;
DataSet cache =(DataSet) context.Cache["employee"];
if (cache == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return GetData();
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return cache;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DataSet GetData()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string connStr = ConfigurationManager.ConnectionStrings["mySource"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
SqlDataAdapter adp = new SqlDataAdapter("select id,name from dbo.employee", conn);
SqlCacheDependency dep = new SqlCacheDependency(adp.SelectCommand);
DataSet ds=new DataSet();
adp.Fill(ds);
context.Cache.Add("employee", ds, dep, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(this.DataDiff));
return ds;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void DataDiff(string key, object value, CacheItemRemovedReason reason)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("key:" + key);
GetData();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
这里需要注意的是 select语句的写法, 不能使用 select * 的方式,一定要在表名前加架构名称 如我们这里的 dbo.employee.
5.编写测试页面代码.
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" >
</asp:GridView>
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6.插入后台代码
using System;
using System.Data;
using System.Configuration;
using System.Web.Caching;
using System.Data.SqlClient;
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public partial class _Default : System.Web.UI.Page
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EmployeeData em=new EmployeeData();
GridView1.DataSource = em.GetCacheData();
GridView1.DataBind();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}