加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

同步更新缓存中的数据集

发布时间:2020-12-12 16:03:27 所属栏目:MsSql教程 来源:网络整理
导读:将一个取自SQL?Server的数据集放入缓存中了现在想数据库更新的时候缓存中的数据集同步更新? 第一步 修改web,config !--定义数据库连接-- ?connectionStrings ??add?name="NorthwindConnectionString"?connectionString="Server=USERRYR/DB;Database=Northwin
将一个取自SQL?Server的数据集放入缓存中了现在想数据库更新的时候缓存中的数据集同步更新?


第一步

修改web,config

<!--定义数据库连接-->
?<connectionStrings>
??<add?name="NorthwindConnectionString"?connectionString="Server=USERRYR/DB;Database=Northwind;UID=sa;pwd=密码"?providerName="System.Data.SqlClient"/>
?</connectionStrings>
?<system.web>
????<!--?定义缓存策略-->
??<caching>
???<sqlCacheDependency?enabled="true"?pollTime="10000">
????<databases>
??????????<!--
??????????name:必需的?String?属性。
??????????????要添加到配置集合中的?SqlCacheDependencyDatabase?对象的名称。
??????????????此名称用作?@?OutputCache?指令上?SqlDependency?属性的一部分。
??????????pollTime:设置?SqlCacheDependency?轮询数据库表以查看是否发生更改的频率(以毫秒计算)。这儿是一个测试,所以设为10秒,请加大此值
??????????-->
?????<add?connectionStringName="NorthwindConnectionString"?name="Categories"/>
????</databases>
???</sqlCacheDependency>
??</caching>
?</system.web>

第二步.定义cachedData测试类

using?System;
using?System.Data;
using?System.Configuration;
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;
using?System.Data.SqlClient;

///?<summary>
///?Summary?description?for?CachedData
///?</summary>
public?class?CachedData
{
????private?string?Key;
????private?string?_Source;
????///?<summary>
????///?指示数据从哪儿读取的
????///?</summary>
????public?string?Source?{?get?{?return?_Source;?}?}
?public?CachedData()
?{
????????Key?=?"Categories";
????????_Source?=?"未知";
?}

????//读取数据
????public?DataView?getFromCache()?{
????????if?(HttpRuntime.Cache[Key]?==?null)
????????{
????????????//取数据
????????????SqlConnection?conn?=?new?SqlConnection();
????????????conn.ConnectionString?=?ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
????????????SqlCommand?comm?=?new?SqlCommand("SELECT?[CategoryID],?[CategoryName],?[Description]?FROM?[Categories]",?conn);
????????????SqlDataAdapter?sda?=?new?SqlDataAdapter(comm);
????????????DataSet?ds?=?new?DataSet();
????????????conn.Open();
????????????sda.Fill(ds);
????????????DataView?dv?=?ds.Tables[0].DefaultView;
????????????conn.Close();

????????????//启用更改通知
????????????SqlCacheDependencyAdmin.EnableNotifications(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
????????????//连接到?SQL?Server?数据库并为?SqlCacheDependency?更改通知准备数据库表
????????????SqlCacheDependencyAdmin.EnableTableForNotifications(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString,?"Categories");
????????????//制定缓存策略
????????????SqlCacheDependency?scd?=?new?SqlCacheDependency("Categories",?"Categories");
????????????//插入缓存
????????????HttpRuntime.Cache.Insert(Key,?dv,?scd);
????????????_Source?=?"Database";
????????????return?dv;
????????}
????????else?{
????????????//从缓存中取值
????????????_Source?=?"cache";
????????????return?(DataView)HttpRuntime.Cache[Key];
????????????
????????}
????}
}


3.测试页面

<%@?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>Untitled?Page</title>
</head>
<body>
????<form?id="form1"?runat="server">
????????<asp:Label?ID="Label1"?runat="server"?Text="Label"></asp:Label>
????????<asp:DataGrid?runat="server"?ID="Repeater1"></asp:DataGrid>&nbsp;
????</form>
</body>
</html>
其对应cs文件 using?System; using?System.Data; using?System.Data.SqlClient; using?System.Configuration; 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) ????{ ???????? ????????????BindRepeater1(); ???????? ????} ????private?void?BindRepeater1(){ ????????CachedData?cd=new?CachedData(); ????????this.Repeater1.DataSource?=?cd.getFromCache(); ????????this.Repeater1.DataBind(); ????????this.Label1.Text?=?cd.Source; ????} }?? ?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读