Tuesday, August 17, 2010

SQL Table Caching

Step 1:
Enabling the Database and Table
aspnet_regsql.exe -S "192.168.0.181" -U sa -P sa123 -d DMS -ed
aspnet_regsql.exe -S "192.168.0.181" -U sa -P sa123 - d DMS -t Userlogin -et

Step 2:
< connectionStrings >
< add name="GetitDMConnectionString2" connectionString="Data Source=192.168.0.181;Initial Catalog=DMS;Persist Security Info=True;User ID=sa;Password=sa123;Connect Timeout=900; pooling='true';Max Pool Size=200" providerName="System.Data.SqlClient"/>
< /connectionStrings >

< system.web >
< caching >
< sqlCacheDependency enabled="true" >
< databases >
<add name="DMS" connectionStringName="GetitDMConnectionString2" pollTime="500" />
< /databases >
< /sqlCacheDependency >
< /caching >


Step 3:
using System.Data.SqlClient;
using System.Web.Caching;

DataSet mycustomers;
mycustomers = (DataSet)Cache["firmCustomers"];
if (mycustomers == null)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["GetitDMConnectionString2"].ConnectionString);

SqlDataAdapter da = new SqlDataAdapter("Select * from userlogin", conn);
mycustomers = new DataSet();
da.Fill(mycustomers);
SqlCacheDependency myDependency = new SqlCacheDependency("DMS", "userlogin");
Cache.Insert("firmCustomers", mycustomers, myDependency);
Label1.Text = "Producing From database";
}
else
{
Label1.Text = "Produced From Cache";
}
GridView1.DataSource = mycustomers;
GridView1.DataBind();

No comments: