Working With SQL Dependency Using Caching

Note: I assume that reader of this blog know about caching in ASP.NET and they have knowledge to use SQL Server.
 
Let's start to work with SQL dependency in caching, using ASP.NET.

Step 1: Enable caching in database and table
 
Enable dependencies on the database table.
  1. aspnet_regsql -S <server_name> -U <user_name> -P <password> -d <database_name> -t <table_name> -et  
Step 2: Enable Service broker in SQL Server.
  1.  ALTER DATABASE <database_name>SET ENABLE_BROKER  
  2.         
  3. EXEC sp_configure 'show advanced options''1'   
  4.         
  5. RECONFIGURE   
  6.         
  7. EXEC sp_configure 'clr enabled', 1   
  8.          
  9.  RECONFIGURE    
Step 3: Modify your web.config to enable the caching.
  1. <configuration>  
  2.     <connectionStrings>  
  3.         <add name="ConnStr" connectionString="Data Source=<server_name>;Initial Catalog=<database_name>;Persist Security Info=True;User ID=<user_name>;Password=<password>" />  
  4.     </connectionStrings>  
  5.     <system.web>  
  6.         <compilation debug="true" targetFramework="4.0" />  
  7.   
  8.         <caching>  
  9.             <sqlCacheDependency enabled="true">  
  10.                 <databases>  
  11.                     <add name="EMS" connectionStringName="ConnStr" pollTime="500" />  
  12.                 </databases>  
  13.             </sqlCacheDependency>  
  14.         </caching>  
  15.     </system.web>  
  16. </configuration>  
Step 4: Get the data from the database to test SQL dependency. 
  1. using System.Data.SqlClient;  
  2. using System.Data;   
Note: These two namespace are required to use ADO.NET to get the data from the database, using System.Web.Caching.
Note: This namespace is used to get cache object in ASP.NET, using System.Configuration.
Note: This namespace used to get the connection string information from web.config file.
  1. public partial class Default3 : System.Web.UI.Page  
  2. {  
  3.     protected void Page_Load(object sender, EventArgs e)  
  4.     {  
  5.         if (Cache["emp"] == null)  
  6.         {  
  7.             string str = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;  
  8.             SqlConnection con = new SqlConnection(str);  
  9.               
  10.             SqlDependency.Start(str);  
  11.             SqlCommand com = new SqlCommand("select * from Employee", con);  
  12.             SqlCacheDependency dep = new SqlCacheDependency("EMS","Employee");  
  13.             SqlDataAdapter a = new SqlDataAdapter(com);  
  14.             DataSet ds = new DataSet();  
  15.             a.Fill(ds, "Employee");  
  16.             Cache.Insert("emp", ds, dep, DateTime.Now.AddMinutes(1), TimeSpan.Zero);  
  17.             GridView1.DataSource = ds.Tables[0];  
  18.             GridView1.DataBind();  
  19.             Response.Write("<script>alert('Data comming from Database')</script>");  
  20.         }  
  21.         else  
  22.         {  
  23.             DataSet ds = (DataSet)Cache["emp"];  
  24.             GridView1.DataSource = ds.Tables[0];  
  25.             GridView1.DataBind();  
  26.             Response.Write("<script>alert('Data comming from Cache Memory')</script>");  
  27.         }  
  28.     }  
  29. }   
Note:

First time, when the page is going to load, the value of Cache["emp"] is null. Cache object used to store the data. Second time, when page is going to load, the value of Cache["emp"] contains the data (table information). Thus, the data will load from cache object into control. If any changes will happen into the table, which is already enabled with cache dependency and cache object will process a round trip to the database and re-load cache object with the modified data.
Ebook Download
View all
Learn
View all