Usage of SqlDependency and SqlCacheDependency caches

Source: Internet
Author: User

SqlDependency with Servicebroker for caching

Step One:

The SQL database must have the Servicebroker service turned on, first detecting if Servicebroker is enabled, and the detection method:

Select databasepropertyex (' dbname ', ' isbrokerenabled ')

--1 indicates that 0 is enabled and is not enabled

Step Two:

If Servicebroker is not enabled, use the following statement to enable:

ALTER Database < databases name > SET enable_broker;

Step Three:

In implementing a service-based SQL data cache dependency process, you need to explicitly call Sqldependency.start to start a listener that accepts dependency change notifications.

Sqldependency.start (connectionString);//recommend adding this code to the Application_Start method of Global.asax
Sqldependency.stop (connectionString);//For closing, can be added in Global.asax Application_End method

Step Four: Cache implementation

To implement cached code using SqlDependency:

public class Cachehelper {
static Cache Webcache = HttpContext.Current.Cache;
static string defaultconn = configurationmanager.connectionstrings["Default"]. ConnectionString;
public static DataTable Getsystemparams () {
if (webcache["systemparam"] = = null) {
String strSQL = "Select Usystemparamid,paramname,paramvalue,description from dbo. Dts_systemparam ";
SqlDataAdapter da = new SqlDataAdapter (strSQL, Defaultconn);
SqlDependency dep = new SqlDependency (da. SelectCommand);
Dep. OnChange + = new Onchangeeventhandler (dep_onchange);
DataTable tbl = new DataTable (); Da. Fill (TBL);
webcache["Systemparam"] = TBL;
return TBL;
}
else {
Return (DataTable) webcache["Systemparam"];
}
}
private static void Dep_onchange (object sender, Sqlnotificationeventargs e) {
Webcache.remove ("Systemparam");
}
}

Usage of SqlDependency and SqlCacheDependency caches

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.