Use cache in the gridview

Source: Internet
Author: User

Note: This series records the problems I encountered during actual development and adds some tips and articles to my favorites. The first article starts today.

The tip of this article is to use the gridview control to bind a database and use the Cache Technology to Improve program performance.

// Add this reference using system. Data. sqlclient; protected void page_load (Object sender, eventargs e) {If (! Page. ispostback) {binddata () ;}} private void binddata () {If (Cache ["cache"] == null) {sqlconnection con = new sqlconnection ("Data Source =. \ sqlexpress; attachdbfilename = | datadirectory | northwnd. MDF; Integrated Security = true; user instance = true "); sqldataadapter ad = new sqldataadapter (" select * from categories ", con); dataset DS = new dataset (); Ad. fill (DS); cache. insert ("cache", DS, null, datetime. now. addminutes (2), timespan. zero); gridview1.datasource = Ds;} else gridview1.datasource = (Dataset) cache ["cache"]; gridview1.databind ();}

Main sentence:

Cache.Insert("Cache", ds, null, DateTime.Now.AddMinutes(2), TimeSpan.Zero);

The first parameter is the cache key used to reference the object.
The second parameter is to insert an object in the cache.
Third parameter: The file dependency or cache key dependency of the inserted object. When any dependency item is changed, the object is invalid and removed from the cache. If there is no dependency, this parameter contains the nullnothingnullptrnull reference (nothing in Visual Basic ).
Fourth parameter: the time when the inserted object expires and is removed from the cache. To avoid possible local time issues (such as changing from Standard Time to Daylight Time), use utcnow for this parameter value, rather than now. If absolute expiration is used, the slidingexpiration parameter must be noslidingexpiration.
Fifth parameter: the time interval between the last access to the inserted object and the expiration time of the object. If this value is equivalent to 20 minutes, the object will expire and be removed from the cache 20 minutes after the last access. If you use an adjustable expiration time, the value of the absoluteexpiration parameter must be noabsoluteexpiration.

Source: gridview with cache (2008.3.12)

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.