先看下MSDN对此类的介绍:
在以下两者之间建立关系:一是在 ASP.NET 应用程序的 Cache 对象中存储的项;二是特定 SQL Server 数据库表或 SQL Server 2005 查询的结果。无法继承此类。
命名空间:System.Web.Caching
程序集:System.Web(在 system.web.dll 中)
这是我作的一个例子:
新建一个页面CacheSqlDependency.aspx,加入如下代码
+ expand sourceview plaincopy to clipboardprint?protected void Page_Load(object sender, EventArgs e) { DataTable dt = GetTableFromDB(); dt.TableName = "CacheDependency"; TextWriter writer = new StringWriter(); dt.WriteXml(writer); Response.Write(writer.ToString()); } private DataTable GetTableFromDB() { DataTable result = Cache.Get("cacheStudy") as DataTable; if(result != null) return result; using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ForStudy"].ConnectionString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("select * from CacheDependency")) { cmd.Connection = conn; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = cmd; result = new DataTable(); adapter.Fill(result); SqlCacheDependency dependency = new SqlCacheDependency("ForStudy", "CacheDependency"); Cache.Add("cacheStudy", result, dependency, DateTime.Now.AddMinutes(10), TimeSpan.Zero, CacheItemPriority.Default, null); return result; } } } protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = GetTableFromDB();
dt.TableName = "CacheDependency";
TextWriter writer = new StringWriter();
dt.WriteXml(writer);
Response.Write(writer.ToString());
}
private DataTable GetTableFromDB()
{
DataTable result = Cache.Get("cacheStudy") as DataTable;
if(result != null) return result;
using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ForStudy"].ConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("select * from CacheDependency"))
{
cmd.Connection = conn;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
result = new DataTable();
adapter.Fill(result);
SqlCacheDependency dependency = new SqlCacheDependency("ForStudy", "CacheDependency");
Cache.Add("cacheStudy", result, dependency, DateTime.Now.AddMinutes(10), TimeSpan.Zero, CacheItemPriority.Default, null);
return result;
}
}
}
WebConfig中增加SqlCacheDependency的配置
view plaincopy to clipboardprint?<caching> <sqlCacheDependency enabled="true" pollTime="60000"> <databases> <add name="ForStudy" connectionStringName="ForStudy" pollTime="60000"/> </databases> </sqlCacheDependency> </caching> <caching>
<sqlCacheDependency enabled="true" pollTime="60000">
<databases>
<add name="ForStudy" connectionStringName="ForStudy" pollTime="60000"/>
</databases>
</sqlCacheDependency>
</caching>
开启缓存通知
aspnet_regsql.exe -S ADIMIN-B7575567/SQLEXPRESS -U sa -P sa -ed -d ForStudy -et -t CacheDependency
开启对数据库ForStudy表CacheDependency表的缓存依赖,如果不开启,程序则会报异常:没有开启数据库XXX的缓存依赖,没有开启数据表XXX的缓存依赖
WebConfig配置
<connectionStrings > <add name="ForStudy" connectionString="Data Source=ADIMIN-B7575567/SQLEXPRESS;Initial Catalog=ForStudy;Integrated Security=True"/> </connectionStrings>
这是这个例子中用到的数据库连接
开始测试
数据库中执行
use forstudy
create table CacheDependency
(
id int identity,
desp nvarchar(4000)
)
insert into CacheDependency
values ('一')
第一次访问界面显示:
from db
1 一
在数据库中执行:insert into CacheDependency values ('二')
刷新页面显示:
from cache
1 一
一分钟后再次刷新页面显示:
from cache
在以下两者之间建立关系:一是在 ASP.NET 应用程序的 Cache 对象中存储的项;二是特定 SQL Server 数据库表或 SQL Server 2005 查询的结果。无法继承此类。
命名空间:System.Web.Caching
程序集:System.Web(在 system.web.dll 中)
这是我作的一个例子:
新建一个页面CacheSqlDependency.aspx,加入如下代码
+ expand sourceview plaincopy to clipboardprint?protected void Page_Load(object sender, EventArgs e) { DataTable dt = GetTableFromDB(); dt.TableName = "CacheDependency"; TextWriter writer = new StringWriter(); dt.WriteXml(writer); Response.Write(writer.ToString()); } private DataTable GetTableFromDB() { DataTable result = Cache.Get("cacheStudy") as DataTable; if(result != null) return result; using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ForStudy"].ConnectionString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("select * from CacheDependency")) { cmd.Connection = conn; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = cmd; result = new DataTable(); adapter.Fill(result); SqlCacheDependency dependency = new SqlCacheDependency("ForStudy", "CacheDependency"); Cache.Add("cacheStudy", result, dependency, DateTime.Now.AddMinutes(10), TimeSpan.Zero, CacheItemPriority.Default, null); return result; } } } protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = GetTableFromDB();
dt.TableName = "CacheDependency";
TextWriter writer = new StringWriter();
dt.WriteXml(writer);
Response.Write(writer.ToString());
}
private DataTable GetTableFromDB()
{
DataTable result = Cache.Get("cacheStudy") as DataTable;
if(result != null) return result;
using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ForStudy"].ConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("select * from CacheDependency"))
{
cmd.Connection = conn;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
result = new DataTable();
adapter.Fill(result);
SqlCacheDependency dependency = new SqlCacheDependency("ForStudy", "CacheDependency");
Cache.Add("cacheStudy", result, dependency, DateTime.Now.AddMinutes(10), TimeSpan.Zero, CacheItemPriority.Default, null);
return result;
}
}
}
WebConfig中增加SqlCacheDependency的配置
view plaincopy to clipboardprint?<caching> <sqlCacheDependency enabled="true" pollTime="60000"> <databases> <add name="ForStudy" connectionStringName="ForStudy" pollTime="60000"/> </databases> </sqlCacheDependency> </caching> <caching>
<sqlCacheDependency enabled="true" pollTime="60000">
<databases>
<add name="ForStudy" connectionStringName="ForStudy" pollTime="60000"/>
</databases>
</sqlCacheDependency>
</caching>
开启缓存通知
aspnet_regsql.exe -S ADIMIN-B7575567/SQLEXPRESS -U sa -P sa -ed -d ForStudy -et -t CacheDependency
开启对数据库ForStudy表CacheDependency表的缓存依赖,如果不开启,程序则会报异常:没有开启数据库XXX的缓存依赖,没有开启数据表XXX的缓存依赖
WebConfig配置
<connectionStrings > <add name="ForStudy" connectionString="Data Source=ADIMIN-B7575567/SQLEXPRESS;Initial Catalog=ForStudy;Integrated Security=True"/> </connectionStrings>
这是这个例子中用到的数据库连接
开始测试
数据库中执行
use forstudy
create table CacheDependency
(
id int identity,
desp nvarchar(4000)
)
insert into CacheDependency
values ('一')
第一次访问界面显示:
from db
1 一
在数据库中执行:insert into CacheDependency values ('二')
刷新页面显示:
from cache
1 一
一分钟后再次刷新页面显示:
from cache