SQL Server缓存依赖

 
要利用asp.net2.0中的新SQL Server高速缓存依赖性,必须SQL Server数据库支持该功能.可使用C:/Windows/Microsoft .NET/Framework/V2.0XXXXX/下的aspnet_regsql.exe工具.
1.使数据库支持
aspnet_regsql.exe  - S 服务器地址 -U 数据库用户名  - P 数据库密码  - d 数据库名称  - dd

2.使表支持SQL高速缓存依赖性
aspnet_regsql.exe  -S 服务器地址 -U 数据库用户名   - P 数据库密码  - d 数据库名称  - t 表名  - et

另外
-- 使数据库不支持SQL高速缓存依赖性
aspnet_regsql.exe 
-S 服务器地址 -U 数据库用户名   - P 数据库密码  - d 数据库名称  - ed
-- 使表不支持SQL高速缓存依赖性
aspnet_regsql.exe 
-S 服务器地址 -U 数据库用户名   - P 数据库密码  - d 数据库名称  - t 表名  - dt
-- 查询当前数据库支持SQL高速缓存依赖性的表
aspnet_regsql.exe 
-S 服务器地址 -U 数据库用户名   - P 数据库密码  - d 数据库名称  - lt

3.在<configuration>节点下配置数据库链接字符串
< connectionStrings >< add name = " ConnectionString "  connectionString = " server=服务器;uid=用户名;pwd=密码;database=数据库名称 " /></ connectionStrings >

4.在<system.web>节点下配置
< caching >
        
< sqlCacheDependency enabled = " true "  pollTime = " 10000 " >
          
< databases >
            
< add name = " 数据库名称 "  connectionStringName = " ConnectionString "  pollTime = " 缓存时间,以秒为单位 " /><!-- ConnectionString是变量名称,即上面的数据库链接字符串 -->
          
</ databases >
        
</ sqlCacheDependency >
      
</ caching >

5.客户端代码
<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " Default3.aspx.cs "  Inherits = " Default3 "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head runat = " server " >
    
< title > 无标题页 </ title >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
        
< asp:Label ID = " Label1 "  runat = " server "  Text = " Label " ></ asp:Label >
        
< asp:GridView ID = " GridView1 "  runat = " server " >
        
</ asp:GridView >
    
</ form >
</ body >
</ html >

6.服务端代码
using  System;
using  System.Data;
using  System.Data.SqlClient;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

using  System.Web.Caching;

public   partial   class  Default3 : System.Web.UI.Page
{
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        DataSet dt;
        dt 
=  (DataSet)Cache[ " firmOrders " ];
        
if  (dt  ==   null )
        {
            
string  connStr  =  ConfigurationManager.ConnectionStrings[ " ConnectionString " ].ConnectionString;
            
string  sql  =   " select * from Orders " ;

            SqlConnection conn 
=   new  SqlConnection(connStr);
            SqlDataAdapter da 
=   new  SqlDataAdapter(sql, conn);

            dt 
=   new  DataSet();
            da.Fill(dt);

            SqlCacheDependency myDependency 
=   new  SqlCacheDependency( " Northwind " " Orders " );
            Cache.Insert(
" firmOrders " , dt, myDependency);

            Label1.Text 
=   " 从数据库读取 " ;
        }
        
else
        {
            Label1.Text 
=   " 从内存读取 " ;
        }
        GridView1.DataSource 
=  dt;
        GridView1.DataBind();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值