同步更新缓存中的数据集(转贴)
来源:互联网 发布:学java有用么 编辑:程序博客网 时间:2024/05/22 03:51
将一个取自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;
}
}
- 同步更新缓存中的数据集(转贴)
- 同步更新缓存中的数据集(转贴)
- 同步更新缓存中的数据集
- 同步更新缓存数据
- CacheDependency 实现xml文件与缓存数据同步更新
- 使用数据集缓存更新数据库
- 数据缓存 更新方式
- [SQL] 数据同步更新
- DataGridView数据同步更新
- merge同步更新数据
- 监听数据库中的缓存配置以更新内存中的缓存数据
- 如果缓存中的数据和数据库中的数据不同步的情况下,怎么样把缓存中的数据同步到数据库中?
- asp中的数据缓存
- TP中的数据缓存
- fragment中的数据缓存
- jQuery中的数据缓存
- 同步所有已挂载的文件系统。所有缓存中的数据将被立刻写入磁盘
- MySQL UDF实现缓存数据同步
- 数据分组--聚合函数使用范例(小计,统计,汇总)实例
- .Net 2.0 缓存使用(转贴)
- DLL的调用
- Index and length must refer to a location within the string是什么错?
- sql 查询经典语句
- 同步更新缓存中的数据集(转贴)
- 一些生活经验
- Java影响力远超想像,已占印度2%GDP
- Bash Profile【整理总结】
- SecureCRT自动登陆脚本
- WORD编辑论文的几点技巧
- 在Source Insight中集成SourceStyler
- 细数AJAX应用程序开发的七宗罪
- 项目经理的第一个精神准备