将一个取自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>
</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; } }
第一步
修改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>
</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; } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。