title: SQL Server data caching in ASP.NET
body: <p>I am trying to use caching to save time refreshing my web application and do so automatically whenever the database changes, and am having trouble setting up the cache. </p>
  
  <p>I have added the following code to where I access SQL...</p>
  
  <pre><code>SqlDependency.Start("Data Source=" + serverName + ";" + 
                      "Initial Catalog=...;" + "User Id=...;Password=...;");
  </code></pre>
  
  <p>and then after the SQL connection is made and just before I pull data from SQL...</p>
  
  <pre><code>SqlCacheDependency dependency = new SqlCacheDependency(command);
  
  int numberOfMinutes = 3;
  DateTime expires = DateTime.Now.AddMinutes(numberOfMinutes);
  
  Current.Response.Cache.SetExpires(expires);
  Current.Response.Cache.SetCacheability(HttpCacheability.Public);
  Current.Response.Cache.SetValidUntilExpires(true);
  Current.Response.AddCacheDependency(dependency);
  </code></pre>
  
  <p>Previously in the method that this code is in, I accessed SQL and returned a list of objects from SQL. What further steps do I need to do to set up the cache? I'm assuming I have to add the data to the cache somehow and then retrieve it from the cache somehow? </p>