Cache is. A new thing introduced by net2.0, with SqlCacheDependency (exists in System. web. cacheing) You can add cache item dependencies on database tables or queries. This item can be used in sql2005 without setting the cache dependency conditions because sql2005 has provided, two methods can be used in sql2000:
I. Perform command operations on the database
Use the aspnet_regsql command line tool. The aspnet_regsql tool is located in the Windows \ Microsoft. NET \ Framework \ [version] folder. to configure SqlCacheDependency, run the command line tool. For example:
Aspnet_regsql-S localhost-E-d test-ed // test is the database name
Aspnet_regsql-S localhost-E-d test-t news-et // test indicates the database name, and news indicates the table name.
Ii. Or use the SqlCacheDependencyAdmin class
Method 1:
In the <configuration> section of the web. config file, add:
<ConnectionStrings>
<Add name = "testConn" connectionString = "server =.; database = test; uid = sa; pwd ="/>
</ConnectionStrings>
In the following <system. web> section, add a cache policy as follows:
<Caching>
<SqlCacheDependency enabled = "true" pollTime = "600000">
<Databases>
<Add name = "test" connectionStringName = "testConn" pollTime = "600000"/>
</Databases>
</SqlCacheDependency>
</Caching>
Note: The name in <add> is required and the name of the SqlCacheDependencyDatabase object to be added to the set. This name is used as part of the SqlDependency attribute on the @ OutputCache command. PollTime: sets SqlCacheDependency to poll the database table to check the frequency of changes (in milliseconds ).
Put a GridView on the aspx page. The code file can be as follows:
Protected void Page_Load (object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
String key = "TableCache ";
DataSet data = (DataSet) HttpRuntime. Cache [key];
If (data = null)
{
Data = GetDataSource ();
AggregateCacheDependency cd = TableDependency ();
HttpRuntime. Cache. Add (key, data, cd, DateTime. Now. AddHours (1), System. Web. Caching. Cache. NoSlidingExpiration, CacheItemPriority. High, null );
}
This. GridView1.DataSource = data;
This. GridView1.DataBind ();
}
}
Private AggregateCacheDependency TableDependency ()
{
AggregateCacheDependency dependency = new AggregateCacheDependency ();
Dependency. Add (new SqlCacheDependency ("test", "news "));
Return dependency;
}
Private DataSet GetDataSource ()
{
DataSet ds = SqlHelper. ExecuteDataset (CommandType. StoredProcedure, "GetAllNews ");
Return ds;
}
Protected void GridView1_PageIndexChanging (object sender, GridViewPageEventArgs e)
{
This. GridView1.PageIndex = e. NewPageIndex;
This. GridView1.DataSource = GetDataSource ();
This. GridView1.DataBind ();
}
Method 2: Use the SqlCacheDependencyAdmin class. The web. config file is configured in the same way as method 1.
Protected void Page_Load (object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
String key = "TableCache ";
DataSet data = (DataSet) HttpRuntime. Cache [key];
If (data = null)
{
Data = GetDataSource ();
// Enable change notification for the specified database
SqlCacheDependencyAdmin. EnableNotifications (ConfigurationManager. ConnectionStrings ["testConn"]. ConnectionString );
// Connect to the sqlserver database and prepare the database table for the SqlCacheDependency Change Notification
SqlCacheDependencyAdmin. enabletableforconfigurications (ConfigurationManager. ConnectionStrings ["testConn"]. ConnectionString, "news ");
// Specify the Cache Policy
SqlCacheDependency scd = new SqlCacheDependency ("test", "news ");
// Insert Cache
HttpRuntime. Cache. Insert (key, data, scd );
}
This. GridView1.DataSource = data;
This. GridView1.DataBind ();
}
}
Private DataSet GetDataSource ()
{
DataSet ds = SqlHelper. ExecuteDataset (CommandType. StoredProcedure, "GetAllNews ");
Return ds;
}
Protected void GridView1_PageIndexChanging (object sender, GridViewPageEventArgs e)
{
This. GridView1.PageIndex = e. NewPageIndex;
This. GridView1.DataSource = GetDataSource ();
This. GridView1.DataBind ();
}