惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
GRAHAM CLULEY
T
Tenable Blog
Know Your Adversary
Know Your Adversary
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Privacy International News Feed
S
Security Affairs
NISL@THU
NISL@THU
O
OpenAI News
Attack and Defense Labs
Attack and Defense Labs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Hacker News: Ask HN
Hacker News: Ask HN
Webroot Blog
Webroot Blog
Schneier on Security
Schneier on Security
S
SegmentFault 最新的问题
S
Schneier on Security
G
Google Developers Blog
V
V2EX
C
Check Point Blog
U
Unit 42
Google DeepMind News
Google DeepMind News
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
Recent Announcements
Recent Announcements
M
MIT News - Artificial intelligence
S
Secure Thoughts
博客园 - 司徒正美
Recorded Future
Recorded Future
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
K
Kaspersky official blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
AI
AI
博客园 - 聂微东
N
News and Events Feed by Topic
SecWiki News
SecWiki News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Vulnerabilities – Threatpost
P
Palo Alto Networks Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
Recent Commits to openclaw:main
Recent Commits to openclaw:main
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
The Hacker News
The Hacker News
The Last Watchdog
The Last Watchdog
Project Zero
Project Zero
W
WeLiveSecurity
博客园 - Franky

博客园 - netwom

Apache和IIS共享80端口的四个设置方法 jquery 常用方法 汇总 市场营销理论4P、4C、4R 利用客户端缓存对网站进行优化 应聘感悟 惹恼程序员的十件事[转] T-sql技巧集 [转]Asp.NET生成静态页面并分页 如果以后再也听不见 [转][荐]大型Web2.0站点构建技术初探 网站策划系列——艾瑞黄亮新 Visual C#中操作WMI的类库简介 2007 中国互联网统计简报及推荐IT行业资料 Silverlight [WPF/E] 银光 [荐]今天你多态了吗? C#泛型集合揽胜 .NET Framework 类库提供的命名空间 [荐]深入剖析Server 2003下IIS 6.0 提高ASP.Net应用程序性能的十大方法
asp.net 2.0 + sqlserver2005 数据依赖缓存
netwom · 2008-07-25 · via 博客园 - netwom

Asp.net 2.0 提供了一个新的数据缓存功能,就是利用sql server2005 的异步通知功能来实现缓存

1.首先在sqlserver2005 中创建一个test的数据库.添加一个 employee的数据库表.

1CREATE TABLE [dbo].[employee](
2    [id] [int] IDENTITY(1,1NOT NULL,
3    [name] [varchar](50
4
5

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

3.编写global.asax文件,启动监听sql2005通知事件.

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">

    
void Application_Start(object sender, EventArgs e) 
    
{
        
string connStr=ConfigurationManager.ConnectionStrings["mySource"].ConnectionString;
        SqlDependency.Start(connStr);
    }

    
    
void Application_End(object sender, EventArgs e) 
    
{
        
string connStr = ConfigurationManager.ConnectionStrings["mySource"].ConnectionString;
        SqlDependency.Stop(connStr);
    }

</script>

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;

/// <summary>
/// EmployeeData 的摘要说明
/// </summary>

public class EmployeeData
{
    
public EmployeeData()
    
{
    }


    
private HttpContext context;

    
public DataSet GetCacheData()
    
{
        context 
= HttpContext.Current;
        DataSet cache 
=(DataSet) context.Cache["employee"];
        
if (cache == null)
        
{
            
return GetData();
        }

        
else
        
{
            
return cache;
        }

    }



    
public DataSet GetData()
    
{
        
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;
    }


    
public void DataDiff(string key, object value, CacheItemRemovedReason reason)
    
{
        Console.WriteLine(
"key:" + key);
        GetData();
    }


}

这里需要注意的是 select语句的写法, 不能使用 select *  的方式,一定要在表名前加架构名称 如我们这里的 dbo.employee.

5.编写测试页面代码.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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">
    
<div>
        
<asp:GridView ID="GridView1" runat="server" >
        
</asp:GridView>
    
</div>
    
</form>
</body>
</html>

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;

public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        EmployeeData em
=new EmployeeData();
        GridView1.DataSource 
= em.GetCacheData();
        GridView1.DataBind();
    }


}

TrackBack:http://www.cnblogs.com/yg_zhang/archive/2006/09/20/508961.html


使用需要注意Service Broker 和通知服务不支持某些SQL