Dict.CN 在线词典, 英语学习, 在线翻译 ------------- MyGitee 朱秋贵内科诊所 My腾云code

简单实现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>

posted @ 2009-12-01 15:10  cn2024  阅读(293)  评论(0编辑  收藏  举报