Wednesday, September 23, 2009

SQL Data Caching

using System.Web.Caching;
using System.Data.SqlClient;

public partial class MyCaching : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds;
ds = (DataSet)Cache["firmCustomers"];
if (ds == null)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["GetitDMConnectionString2"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("Select top 20 areaname, pincode from area", conn);
ds = new DataSet();
da.Fill(ds);
SqlCacheDependency myDependency = new SqlCacheDependency("DMS", "area");
Cache.Insert("firmCustomers", ds, myDependency);
Label1.Text = "Produced From database";
}
else
{
Label1.Text = "Produced From Cache";
}
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
============================
Enabling the Database and Table

d:\program files\microsoft visual studio 9.0\VC>

Run these commands in Command promt On above prompt

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
====================================

//webconfig
Symbols
LL => Less Than
GG => Greater Than

< 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 >

No comments: