Thursday, January 7, 2010

Implement Caching

In SQL Server DataBase
:

Run this command:

Exp:
aspnet_regsql.exe -S localhost -U sa -P sa -d NewDataBase -ed
This command Enable your Data Base, now You need to enable using table , for this command is...
aspnet_regsql.exe -S localhost -U sa -P sa -d NewDataBase -t tableName -et
After this go to SQL server , you get a new table "AspNet_SqlCacheTablesForChangeNotification".

coding on web.conf file
system.web
caching
sqlCacheDependency enabled="true"
databases
add name="NewTest" connectionStringName="Connection" pollTime="500"
/databases
/sqlCacheDependency
/caching
/system.web


Coding on .cs file
DataSet geography = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
SqlDataAdapter adapter = new SqlDataAdapter("Select * from Users", conn);

adapter.Fill(geography);
SqlCacheDependency dependency = new SqlCacheDependency("NewTest", "Users"); Cache.Insert("Geo", geography, dependency,System.Web.Caching.Cache.NoAbsoluteExpiration,TimeSpan.FromMinutes(20));
GridView1.DataSource = geography;
GridView1.DataBind();
lblMessage.Text = "Categories retrieved from the Data Base";
}
catch { }
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds = (DataSet)Cache["Geo"]; lblMessage.Text = "Categories retrieved from the Cached data base";
GridView1.DataSource = ds;
GridView1.DataBind();
}

No comments:

Post a Comment