使用sqlCacheDependency

王朝mssql·作者佚名  2006-12-13
窄屏简体版  字體: |||超大  

第一步

修改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;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航