在CMD下启动数据Cache,
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql -C "Data Source=192.168.0.113;Initial Catalog=gft_esp_rd;Persist Security Info=True;User ID=sa;Password=sql2k5" -ed -et -t "Mess_Reader"
注:-C后面是数据库连接, Mess_Reader为要监控的表
//web.config配置<system.web>
//<connectionStrings>
// <add name="Northwind1" connectionString="Data Source=192.168.6.89;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sql2k5" // // providerName="System.Data.SqlClient"/>
// <add name="gft_esp" connectionString="Data Source=192.168.0.113;Initial Catalog=gft_esp_rd;Persist Security Info=True;User ID=sa;Password=sql2k5"/>
// <add name="FE_4d" connectionString="Dsn=FE_4d;uid=administrator;pwd=gft@esp800" providerName="System.Data.Odbc"/>
// <add name="GFE_4d" connectionString="Dsn=GFE_4d;uid=administrator;pwd=gft@esp800" providerName="System.Data.Odbc"/>
// <add name="gft_esp_rdConnectionString" connectionString="Data Source=192.168.0.113;Initial Catalog=gft_esp_rd;User ID=sa;Password=sql2k5" // // providerName="System.Data.SqlClient"/>
// </connectionStrings>
</httpModules>
<caching>
<sqlCacheDependency enabled="true" pollTime="500" >
<databases>
<add connectionStringName="gft_esp" name="Sql_CacheDependency"/><!-- 一个是数据库接字符串,一个是名字 -->
</databases>
</sqlCacheDependency>
</caching>
</system.web>
cs 代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//XML文件监控
//我们在一页面建立1个按钮,查看CACHE是否存在
//在窗体启动时我们创建CACHE,名称="txt2",数值=数据集ds
//该CACHE与myfile.xml相关联,当myfile.xml文件变化时,txt2CACHE就被自动删除
//string FilePath=MapPath("myfile.xml");
//SqlConnection con=new SqlConnection("Uid=sa;database=pubs;");
//SqlDataAdapter da =new SqlDataAdapter("select * from authors",con);
//DataSet ds=new DataSet();
//da.Fill(ds);
//System.Web.Caching.CacheDependency CacheDependencyXmlFile=new System.Web.Caching.CacheDependency(FilePath);
//Cache.Insert("txt2",ds ,CacheDependencyXmlFile);
//}
//为了监视pubs数据库authors表的变化
//我们可以在pubs数据库authors表建立触发器
//一旦authors表中发生增加,删除,修改数据时,触发器会自动修改文件myfile.xml
//一旦myfile.xml文件变化,txt2CACHE就被系统自动删除
//CREATE TRIGGER tr_authors
//ON authors
//FOR INSERT, UPDATE ,delete
//AS
//declare @cmd nvarchar(4000)
//select @cmd='bcp "select convert(nvarchar(30),Getdate(),13)" queryout D:\cache\WebCache\myfile.xml -c -Sglc2403 -Usa -P'
//exec master..xp_cmdshell @cmd
//GO
// private void QueryButton_Click(object sender, System.EventArgs e)
//{
//if ( Cache["txt2"]!=null)
//{
//Response.Write("exists");
//}
//else
//{
//Response.Write("not exists");
//}
//}
//本文来自: IT知道网(http://www.itwis.com) 详细出处参考:http://www.itwis.com/html/net/aspnet/20101030/9432_2.html
// 首先我们点按钮,显示Cache["txt2"]存在
//现在我们去表authors中任意修改一数据,再点按钮,显示Cache["txt2"]不存在拉
//以上我们是把CACHE是和一个文件相关联,我们还可以把CACHE和文件组关联,建立依赖
//以下我们把CACHE和2个文件(myfile.xml,myfile1.xml)关联,一旦文件组中其中任意一文件变化,Cache会把"txt2"项的数据从CACHE中删除
//string[] FilePath=new String[]{MapPath("myfile.xml"),MapPath("myfile1.xml")};
//System.Web.Caching.CacheDependency CacheDependencyXmlFile=new System.Web.Caching.CacheDependency(FilePath);
//string CacheVaule="a";
//Cache.Insert("txt2", CacheVaule ,CacheDependencyXmlFile);
//Cache监控
if (HttpRuntime.Cache["Category"] == null)//不存在则
{
//对GridView1 进行数据绑定
System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency("Sql_CacheDependency", "Mess_Reader");
//声明SqlCacheDependency其中构造函数中的这两个参数(codematic必需与WebConfig配置的sqlCacheDependency的一致,Category则是缓存的key)
HttpRuntime.Cache.Insert("Category", DateTime.Now.ToString(),dep,
System.Web.Caching.Cache.NoAbsoluteExpiration,
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Default, null);
//向缓存集合中插入数据(为了直观我们在缓存中放入的是当前时间当更改表Category时则重新更改缓存中的时间)
this.Label1.Text = HttpRuntime.Cache["Category"].ToString();
//显示缓存Category中的数据
}
else//存在则把缓存数据显示到页面
{
this.Label1.Text = HttpRuntime.Cache["Category"].ToString();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
this.Label1.Text = HttpRuntime.Cache["Category"].ToString(); //显示缓存中的内容
}
---------------------------------------代码 完--------------------------------------------------------------
按以上可以正确配置ASP.NET 高速缓存了,