1. Start the notification database for cache dependencies
Run in the VS Developer command prompt (switch to the directory where Aspnet_regsql.exe is located, sample directory: C:\Windows\Microsoft.NET\Framework64\v4.0.30319):
aspnet_regsql-s. -u sa-p pwd-ed-d xxx-et-t xxx;
Cache dependency Disabled:
Aspnet_regsql-s. \DS-E-dd-d XXX
-s server name-E Integrated Authentication-u account-p password-ed CREATE database for SQL cache dependency-dd disable-D database name-et start table for SQL Cache-T table name
2. Configuring the Web. config file
Add under system.web
<caching> <sqlcachedependency enabled= "true" > <databases> <add name= "Practicedb "Connectionstringname=" constring "polltime="/> </databases> </sqlCacheDependency> </caching>
Then add the corresponding key value under the configuration
3. Inserting a cache with database dependencies
if (cache["key"] = = null) { Nmdb. Dataclassesdatacontext db = new Nmdb. Dataclassesdatacontext (); var r = (from res in DB.T1 select res). ToList (); Cache.Insert ("Key", R, New SqlCacheDependency ("Practicedb", "student")); Repeater1.datasource = cache["key"]; Repeater1.databind ();
Done. The cache is accessed directly every time the database has not changed, which can be confirmed in SQL Server Profiler. It's only when you change the data in the database that you request the database again to get the data.
C # Database Cache dependency