简单实现SQL Server2000数据库缓存

第一步:修改配置文件

  <connectionStrings>
    <add name="ConnectionString"connectionString="Server=(local);database=建立缓存的数据库;uid=sa;pwd=123456"providerName="System.Data.SqlClient" />
  </connectionStrings>

    <!-- 定义缓存策略-->
    <caching>
      <sqlCacheDependency enabled="true" pollTime="10000">        
        <databases>
          <add connectionStringName="ConnectionString" name="建立缓存的数据库"/>
        </databases>
      </sqlCacheDependency>
    </caching>

第二步: 建立缓存

SqlCacheDependencyAdmin.EnableNotifications(数据库连接字符串);
//连接到 SQL Server 数据库并为 SqlCacheDependency 更改通知准备数据库表
SqlCacheDependencyAdmin.EnableTableForNotifications(数据库连接字符串, 表名,用来说明数据库中哪些表更新时 重新建立缓存);
//定制缓存策略
SqlConnectionStringBuilder ConnectionStringBuilder = new SqlConnectionStringBuilder(数据库连接字符串);
//建立依赖性
SqlCacheDependency CacheDependency = new SqlCacheDependency(数据库名(必须与配置文件中的配置的名字一致),进行缓村的表名);

SqlDataAdapter Adapter = newSqlDataAdapter
Adapter.SelectCommand = "select * from 进行缓村的表名";
DataTable dt = new DataTable();
 Adapter.Fill(dt);

if(HttpRuntime.Cache[应用程序缓存名]==null)

{
HttpRuntime.Cache.Add(应用程序缓存名, dt, CacheDependency,Cache.NoAbsoluteExpiration, new TimeSpan(1, 0, 0),CacheItemPriority.High, null);
return dt;

}
else
{
return (DataTable)HttpRuntime.Cache[应用程序缓存名];
}

 

两步骤搞定非常简单_-_

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

using System;
using System.Collections.Generic;
using System.Web.Caching;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication2
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlCacheDependencyAdmin.EnableNotifications("testConnectionString");
            //连接到 SQL Server 数据库并为 SqlCacheDependency 更改通知准备数据库表
            SqlCacheDependencyAdmin.EnableTableForNotifications("testConnectionString", "user");
            //定制缓存策略
            SqlConnectionStringBuilder ConnectionStringBuilder = new SqlConnectionStringBuilder("testConnectionString");
            //建立依赖性
            SqlCacheDependency CacheDependency = new SqlCacheDependency("test", "user");


            SqlDataAdapter Adapter = new SqlDataAdapter();
            Adapter.SelectCommand.CommandText = "select * from user";
            DataTable dt = new DataTable();
            Adapter.Fill(dt);
            if (HttpRuntime.Cache["user"] == null)
            {
                HttpRuntime.Cache.Add("user", dt, CacheDependency, Cache.NoAbsoluteExpiration, new TimeSpan(1, 0, 0), CacheItemPriority.High, null);
                int dd = dt.Rows.Count;
            }
            else
            {
                DataTable tdt=(DataTable)HttpRuntime.Cache["user"];
                int kk = tdt.Rows.Count;
            }
        }
    }
}

<appSettings/>
    <connectionStrings>
        <add name="testConnectionString" connectionString="Data Source=123123131\SQL2005;Initial Catalog=test;User ID=sa;Password=sa" providerName="System.Data.SqlClient"/>
    </connectionStrings>

    <system.web>
    <!-- 定义缓存策略-->
    <caching>
      <sqlCacheDependency enabled="true" pollTime="10000">
        <databases>
          <add connectionStringName="testConnectionString" name="test"/>
        </databases>
      </sqlCacheDependency>
    </caching>

转载于:https://www.cnblogs.com/smallfa/archive/2009/12/01/1614592.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值