将一个取自SQLServer的数据集放入缓存中了现在想数据库更新的时候缓存中的数据集同步更新第一步修改web,config<!--定义数据库连接--><connectionStrings><addname="NorthwindConnectionString"connectionString="Server=USERRYRDB;Database=Northwind;UID=sa;pwd=密码"providerName="System.Data.SqlClient"/></connectionStrings><system.web><!--定义缓存策略--><caching><sqlCacheDependencyenabled="true"pollTime="10000"><databases><!--name:必需的String属性。要添加到配置集合中的SqlCacheDependencyDatabase对象的名称。此名称用作@OutputCache指令上SqlDependency属性的一部分。pollTime:设置SqlCacheDependency轮询数据库表以查看是否发生更改的频率(以毫秒计算)。这儿是一个测试,所以设为10秒,请加大此值--><addconnectionStringName="NorthwindConnectionString"name="Categories"/></databases></sqlCacheDependency></caching></system.web>第二步.定义cachedData测试类usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Web.Caching;usingSystem.Data.SqlClient;/**////<summary>///SummarydescriptionforCachedData///</summary>publicclassCachedData...{privatestringKey;privatestring_Source;/**////<summary>///指示数据从哪儿读取的///</summary>publicstringSource...{get...{return_Source;}}publicCachedData()...{Key="Categories";_Source="未知";}//读取数据publicDataViewgetFromCache()...{if(HttpRuntime.Cache[Key]==null)...{//取数据SqlConnectionconn=newSqlConnection();conn.ConnectionString=ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;SqlCommandcomm=newSqlCommand("SELECT[CategoryID],[CategoryName],[Description]FROM[Categories]",conn);SqlDataAdaptersda=newSqlDataAdapter(comm);DataSetds=newDataSet();conn.Open();sda.Fill(ds);DataViewdv=ds.Tables[0].DefaultView;conn.Close();//启用更改通知SqlCacheDependencyAdmin.EnableNotifications(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);//连接到SQLServer数据库并为SqlCacheDependency更改通知准备数据库表SqlCacheDependencyAdmin.EnableTableForNotifications(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString,"Categories");//制定缓存策略SqlCacheDependencyscd=newSqlCacheDependency("Categories","Categories");//插入缓存HttpRuntime.Cache.Insert(Key,dv,scd);_Source="Database";returndv;}else...{//从缓存中取值_Source="cache";return(DataView)HttpRuntime.Cache[Key];}}}3.测试页面<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>UntitledPage</title></head><body><formid="form1"runat="server"><asp:LabelID="Label1"runat="server"Text="Label"></asp:Label><asp:DataGridrunat="server"ID="Repeater1"></asp:DataGrid> </form></body></html>其对应cs文件usingSystem;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;publicpartialclass_Default:System.Web.UI.Page...{protectedvoidPage_Load(objectsender,EventArgse)...{BindRepeater1();}privatevoidBindRepeater1()...{CachedDatacd=newCachedData();this.Repeater1.DataSource=cd.getFromCache();this.Repeater1.DataBind();this.Label1.Text=cd.Source;}}