2011-12-16 32 views
1

在实现非常基本的SqlCacheDependency原型时遇到问题。SqlCommand不起作用的SqlCacheDependency

已经尝试过使用sproc,但现在我只能直接执行命令。

public SqlCommand GetReadCommand(int ID) 
    { 
     SqlCommand cmd = new SqlCommand("SELECT dbo.Entity.Entity_ID, dbo.Entity.Name FROM dbo.Entity WHERE Entity_ID = @ID", _conn); 
     cmd.CommandType = CommandType.Text; 
     cmd.Parameters.Add(new SqlParameter("ID", ID)); 

     return cmd; 
    } 

    public SqlCacheDependency GetSqlDependency(int ID) 
    { 
     SqlCommand cmd = GetReadCommand(ID); 
     SqlCacheDependency dep = new SqlCacheDependency(cmd); 
     return dep; 
    } 

而且我读的所有对象的数据集和一些数据创建一个非常简单的依赖测试我手动插入:

public DataSet SelectAll() 
    { 
     DataSet result; 
     if (_cache["Entity_FullSet"] == null) 
     { 
      SqlCommand cmd = new SqlCommand("dbo.GetAllEntities", _conn); 
      cmd.CommandType = CommandType.StoredProcedure; 
      _conn.Open(); 

      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      result = new DataSet(); 
      da.Fill(result); 


      _cache[MasterCacheKey()] = DateTime.Now; 
      string[] mk = new[] { MasterCacheKey() }; 
      CacheDependency cd = new CacheDependency(null, mk); 

      SqlCacheDependency scd = new SqlCacheDependency(GetReadCommand(1)); 
      CacheDependency[] cds = new[] { cd, scd }; 

      AggregateCacheDependency acd = new AggregateCacheDependency(); 
      acd.Add(acd); 

      _cache.Insert("Entity_FullSet", result, scd); 
      _conn.Close(); 
     } 
     else 
     { 
      result = (DataSet)_cache["Entity_FullSet"]; 
     } 
     return result; 
    } 

“主键”是存在的测试总缓存依赖 - 改变它的效果很好,但1(变量scd)上的sql依赖关系根本不起作用。如果我去更新表 - 甚至删除行 - 什么都没有发生,缓存不会被清除。

enter image description here

任何想法,为什么SQL依赖性不登记/发射将不胜感激!

回答

1

您是否在数据库中启用了服务代理,并打开了查询通知?

ALTER DATABASE [YourDB] SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE 

在的Application_Start():

SqlDependency.Start(connectionString) 

并调用停止()在Application_End()。

您可以发布存储过程的代码吗?你有没有在你的SP开始时有SET NOCOUNT ON?如果是这样,那将阻止查询通知。

对于上面发布的代码,请尝试用一个或两个部分名称替换那些包含三部分的列名称。例如,将“dbo.Entity.Entity_ID”替换为“Entity_ID”或(更好)“e.Entity_ID”(将别名“e”分配给dbo.Entity后)。

+0

我还没有设置这些东西 - 也许我错过了一些显而易见的东西,但是在读取类文档时,我不记得那些alter database语句。现在尝试。 – 2011-12-17 20:18:00