Click here to Skip to main content
15,663,557 members
Articles / Web Development / ASP.NET
Posted 21 Mar 2007


25 bookmarked

Caching with SQL server in ASP.NET 2.0

Rate me:
Please Sign up or sign in to vote.
1.42/5 (13 votes)
17 May 20072 min read
use caching to reduce postback time in 2.0


This type of SQL cacheing invalidation is working only with SQL server 7 or above.

Caching has been improved in ASP.NET 2.0. The most interesting feature is the introduction of database-triggered cache invalidation.

Three ways to do this SQL based invalidation.

1. Declarative Output caching by using the OutputCache directive.

2. Programmatic Output caching by using SqlCacheDependency object.

3. Cache API

In old framework 1.x there was polling mechanism for checking invalidity of data, but in Framework 2.0 and SQL server 2005 this process is completely reverse, means now sql server 2005 will notify asp pages about change in data bu using IIS posrt 80 by sending HTTP request.

You can configure SQL Server 2005 to notify your ASP.NET application whenever changes have been made to a database, a database table, or a database row.

To configure SQL server for cacheing we can use SqlCacheDependencyAdmin class,

The SqlCacheDependencyAdmin class has five important methods:

  • DisableNotifications—Disables SQL Cache Invalidation for a particular database.
  • DisableTableForNotifications—Disables SQL Cache Invalidation for a particular table in a database.
  • EnableNotifications—Enables SQL Cache Invalidation for a particular database.
  • EnableTableForNotifications—Enables SQL Cache Invalidation for a particular table in a database.
  • GetTablesEnabledForNotifications—Returns a list of all tables enabled for SQL Cache Invalidation.

In ASP.NET 2.0 we can also create custome cache dependency.

Using the code

//First you have add some lines into your Web.config file
    <add name="mySqlServer" 
      connectionString="Server=localhost;Database=Pubs" />

      <sqlCacheDependency enabled="true">
            pollTime="60000" />
//Within the <databases> subsection, you can list one or more databases that
//you want to poll for changes

Using SQL Cache Invalidation with Page Output Caching

 <%@ OutputCache SqlDependency="Pubs:Titles" 
    Duration="6000" VaryByParam="none" %>
<head runat="server">
    <title>Output Cache Titles</title>
    <form id="form1" runat="server">
    <%= DateTime.Now %>

      Runat="Server" />    
      SelectCommand="Select * FROM Titles"
      ConnectionString="<%$ ConnectionStrings:mySqlServer %>"
      Runat="Server" />
//Notice that the SqlDependency attribute references the name of the 
//database defined within the Web configuration file. 

Using SQL Cache Invalidation with the DataSource Control

//You should consider using SQL Cache Invalidation with the DataSource 
//controls when you need to work with the same database data in multiple 
//pages. The SqlDataSource, AccessDataSource, and ObjectDataSource controls 
//all support a SqlCacheDependency property.
<head id="Head1" runat="server">
    <title>SqlDataSource Caching</title>
    <form id="form1" runat="server">

        <%= DateTime.Now %>

            Runat="server" />
            SelectCommand="select * from titles"
            ConnectionString="<%$ ConnectionStrings:mySqlServer %>"
            Runat="server" />

Caching has a dramatic impact on the performance of database-driven Web applications. Fortunately, the ASP.NET 2.0 framework includes a number of significant new enhancements that make it easier to take advantage of caching in your applications.

The new DataSource controls include properties that make it easy to cache database data in memory. By taking advantage of the DataSource controls, you can retrieve database data and cache the data without writing a single line of code.

Points of Interest

I like to Share my knowledge with guys like you, because i am also one of you, that if i dont know anything then i come to keep exchange of knowledge...


keep attached with my simple way series articles....


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Software Developer
India India
Chirag Patel, a Programmer Analyst in a well known IT company working on .NET Technologies since last 2 years. He is interested in Pure business logic and fuzzy logic. his area of interest is in C#.NET, VB.NET and MSSQL 2005.

catch me on:

Comments and Discussions

GeneralSQL Cache Pin
pradeepvpanzade26-Mar-09 7:57
pradeepvpanzade26-Mar-09 7:57 
QuestionHow it works Pin
DeltaSoft29-Mar-07 2:08
DeltaSoft29-Mar-07 2:08 
GeneralMissleading title Pin
Urs Enzler26-Mar-07 21:34
Urs Enzler26-Mar-07 21:34 
GeneralRe: Missleading title Pin
JustChiragPatel27-Mar-07 17:36
JustChiragPatel27-Mar-07 17:36 
Ok thanks for your suggetion, i wil defenetly change it...

Chirag Patel

GeneralA little more work Pin
Not Active22-Mar-07 4:42
mentorNot Active22-Mar-07 4:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.