• SqlCacheDependency


    SqlCacheDependency

    web页面代码片段:

    web页面代码
    String connectionString = "Data Source=localhost;Initial Catalog=BalloonShop;Integrated Security=True";
    DataTable dt
    = (DataTable)HttpContext.Current.Cache["Customer_test"];
    if (dt == null)
    {
    using (SqlConnection conn = new SqlConnection(connectionString))
    {
    String sqlStr
    = "SELECT dbo.Product.ProductID, Name, col_name FROM" +
    " dbo.Product inner join dbo.ProductCategory on " +
    " dbo.Product.ProductID = dbo.ProductCategory.ProductID inner join" +
    " dbo.tb_category on dbo.ProductCategory.CategoryID = dbo.tb_category.col_id";
    conn.Open();
    SqlCommand command
    = new SqlCommand();
    command.Connection
    = conn;
    command.CommandText
    = sqlStr;
    // DataTableCache.Dependency = new SqlCacheDependency(command);
    SqlCacheDependency dependency = new SqlCacheDependency(command);
             SqlDataAdapter adapter
    = new SqlDataAdapter(command);
    DataSet ds
    = new DataSet();
    adapter.Fill(ds,
    "Customers");
    dt
    = ds.Tables[0];
    // command.Connection.Close();
    DataTableCache.AddCache("Customer_test", dt, command);//缓存通知失效 不起作用
         DataTableCache.AddCache("Customer_test", dt,
    dependency );// 成功
    // HttpContext.Current.Cache.Insert("Customer_test", dt, dependency);

    }
    }

    gvwCustomers.DataSource
    = dt;
    gvwCustomers.DataBind();

    DataTableCahche的代码片段:

    DataTableCache代码
    public class DataTableCache
    {
    private DataTableCache(){ }
    private static SqlCacheDependency dependency = null;

    public static SqlCacheDependency Dependency {
    get { return dependency; }
    set { dependency = value; }
    }
    public static void AddCache(String key, DataTable dt,SqlCommand command) {
    dependency
    = new SqlCacheDependency(command);
    HttpContext.Current.Cache.Insert(key, dt, dependency);
    }
    public static void AddCache(String key, DataTable dt, SqlCacheDependency dependency) {
    HttpContext.Current.Cache.Insert(key, dt, dependency);
    }
    public static void AddCache(String key, DataTable dt) {
    HttpRuntime.Cache.Insert(key,dt, dependency);
    }
    }
    当我使用上面红色的部分插入cache的时候,我改变数据库中的数据时,界面上的数据却不会变化,通知失效机制不能成功运行,
    当我使用蓝色的部分插入cache的时候,
    我改变数据库中的数据时,界面上的数据能发生变化,通知失效机制能成功运行,我很困惑,这到底是什么原因?
    小丁
    菜鸟二级
    专家分-301
    提问于2010-01-19 11:57
    所有回答(2)
    0 0
    这个有问题


    private static SqlCacheDependency dependency = null;

    public static SqlCacheDependency Dependency {
    get { return dependency; }
    set { dependency = value; }
    }
    public static void AddCache(String key, DataTable dt,SqlCommand command) {
    dependency
    = new SqlCacheDependency(command);
    HttpContext.Current.Cache.Insert(key, dt, dependency);
    }

    应该是你这边全局static有点问题,更改成静态单件构造函

    你更改成试试:

     public static void AddCache(String key, DataTable dt,SqlCommand command) {       
    SqlCacheDependency
    _dependency = new SqlCacheDependency(command);
    HttpContext.Current.Cache.Insert(key, dt, _dependency);
    }



    persialee
    老鸟四级
    专家分-3217
    回答于2010-01-19 15:47
    这个我试过,好像也不行,
    还有就是问题出在command上,在command执行以后,比如执行以下语句之后:
    command.ExecuteReader();

    SqlDataAdapter adapter = new SqlDataAdapter(command);
    在把command赋给SqlCacheDependency时就会不起作用,
    而在command执行之前赋给SqlCacheDependency就可以,
    但我不太清楚这里边的运行机制
    评论于2010-01-19 16:59
    1 0

    看看这里
    http://www.dotnetcurry.com/ShowArticle.aspx?ID=263&AspxAutoDetectCookieSupport=1

    的确需要在 cmd.ExecuteNonQuery();之前执行

      SqlCacheDependency dependency = new SqlCacheDependency(cmd);

  • 相关阅读:
    java 14 -7 Date
    java 14 -6 BigInteger和BigDecimal
    java 14 -5 System类
    java14-4 Pattern和Matcher类的使用
    java 14-3 正则表达式的分割
    转:StringBuilder与StringBuffer的区别(转)
    kafka之config/server.properties配置参数说明
    Kafka内核理解:消息的收集/消费机制
    kafka删除topic及其相关数据
    kafka使用问题解决
  • 原文地址:https://www.cnblogs.com/encounter/p/2188809.html
Copyright © 2020-2023  润新知