views:

86

answers:

1

I need to implement cache dependency on a sql select command that selects one value (latest modified date of some rows).

My problem is that although I update the content and when I run the query manually, I see the new date, the system doesn't think it changed and doesn't refresh the content.

    object Taxonomy = GetTaxonomy();

    string connectionString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;

using (SqlConnection connection = new SqlConnection(connectionString)) {

   AggregateCacheDependency aggregate = new AggregateCacheDependency();
   connection.Open();

   using (SqlCommand command1 = new SqlCommand("select max(taxonomy_date_modified) as LastModified from dbo.taxonomy_tbl where .... ") {

      command1.Parameters.Add(new SqlParameter("@TaxonomyId", SqlDbType.Int));
      command1.Parameters[0].Value = siteTaxId;
      command1.Parameters.Add(new SqlParameter("@TaxonomyLanguage", SqlDbType.Int));
      command1.Parameters[1].Value = LanguageHelper.LanguageCode;                       

      SqlCacheDependency dependency = new SqlCacheDependency(command1);

      object modified_date = command1.ExecuteScalar();
      aggregate.Add(dependency);
    }

    cacheTaxonomy.Add(LanguageHelper.LanguageCode, _Taxonomy);

    HttpContext.Current.Cache.Insert("Taxonomy", cacheTaxonomy, aggregate, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(24, 0, 0));

      connection.Close();
A: 

The SQL you're using for the dependency is invalid, you can not use MAX(). SqlCacheDependency is pretty picky on what you can/can not use, see MSDN: Special Considerations When Using Query Notifications for more details

"...The statement must not use any of the following aggregate functions: AVG, COUNT(), MAX, MIN, STDEV, STDEVP, VAR, or VARP..."*

shockstump

related questions