When using SqlDependency to subscribe to query notifications, you must provide SQL Server Service Broker with the query statement for rule-making. Generally, it must be a simple SQL query statement (not available *, not top, you cannot use functions, including Aggregate functions, subqueries, including subqueries after where, external connections, self-connections, temporary tables, and variables, views cannot be used, databases cannot be broken down, and table names must be prefixed with dbo database owners ),
For example, select * from table1, select column1 from table1, select count (*) from table1 are all incorrect SQL query statements. select column1 from dbo. table1 is the correct statement.
Procedure
1. Enable SqlCacheDependency for the database
Method 1: Use the aspnet_regsql command line or SqlCacheDependencyAdmin to configure database connection.
Aspnet_regsql-s SQL server name-E-d database-ed start the data cache dependency function of the database
We also need to execute the aspnet_regsql command on the relevant data table, such:
Aspnet_regsql-s SQL server name-E-d database-t table name-et multiple tables are executed multiple times
Method 2:
SqlCacheDependencyAdmin. EnableNotifications (connectionString); // starts the data cache dependency function of the database.
SqlCacheDependencyAdmin. EnableTableForNotifications (connectionString, table); // enable data table caching
We recommend that you write this code in the Application_Start method of Global. asax to enable the cache dependency function of the database and data table when the application starts.
2. Configure Web. config
<! -- Cache configuration -->
<Caching>
<! -- PollTime is the interval for querying whether the database is changed, in milliseconds -->
<SqlCacheDependency enabled = "true" pollTime = "10000">
<Databases>
<Add name = "Database name" connectionStringName = "Webconfig connection string key" pollTime = "10000"/>
</Databases>
</SqlCacheDependency>
</Caching>
Three-encoding implementation
Cache operation class:
/// <Summary>
/// Obtain all related configurations and add the CacheDependency object
/// </Summary>
/// <Param name = "configKey"> specify the Webconfig's key for the database table name to use the Cache </param>
/// <Returns> </returns>
Public static AggregateCacheDependency GetDepandency (string configKey)
{
AggregateCacheDependency dependency = new AggregateCacheDependency ();
String dbName = ConfigurationManager. receivettings ["CacheDatabaseName"];
String tableConfig = ConfigurationManager. configurettings [configKey];
String [] tables = tableConfig. Split (',');
Foreach (string tableName in tables)
Dependency. Add (new SqlCacheDependency (dbName, tableName ));
Return dependency;
}
Cache service class:
/// <Summary>
/// Link to the SqlCacheDependency class
/// </Summary>
Public class LinksProxy
{
/// <Summary>
/// Set the cache duration
/// </Summary>
Private static readonly int CacheTimeout = int. Parse (ConfigurationManager. deleettings ["LinksCacheDuration"]);
/// <Summary>
/// Whether to enable Cache
/// </Summary>
Private static readonly bool enableCaching = bool. Parse (ConfigurationManager. deleettings ["EnableCaching"]);
/// <Summary>
/// Get all links
/// </Summary>
Public static IList <LinksInfo> GetLinks ()
{
// If the cache is not enabled, the DAL method is called directly to perform common operations.
If (! EnableCaching)
Return LinksDAL. SelectLinks ();
Else // enable Cache
{
// Set the cache key. It is best to set it based on the query conditions so that data of the same condition can be cached.
String key = "Links_by_search_all ";
// Read the cached data
IList <LinksInfo> data = (IList <LinksInfo>) HttpRuntime. Cache [key];
If (data = null) // if no data is read in the cache
{
// Access the DAL to read data from the database, add the data to the cache, and finally return
Data = LinksDAL. SelectLinks ();
AggregateCacheDependency acd = TableCacheDependency. GetDepandency ("LinksTableDependency ");
HttpRuntime. Cache. Add (key, data, acd, DateTime. Now. AddMinutes (CacheTimeout), Cache. nosdomainingexpiration, CacheItemPriority. High, null );
}
Return data;
}
}
}