
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Tying Database Tables to the Cache
|
807
your desktop and web applications blur even further and use the power of a rich cli-
ent combined with web flexibility.
See Also
See the “WebBrowser Class” topic in the MSDN documentation.
14.10 Tying Database Tables to the Cache
Problem
You want to cache datasets you create from a database to help the performance of
your ASP.NET application, but you want changes to the data in the database to be
reflected in your pages.
Solution
Use the SqlCacheDependency class to expire data in the cache when the underlying
database data changes. A
SqlCacheDependency sets up a relationship with the data-
base so that, if the data changes, the item in the cache that has this dependency is
released from the cache, and the code that established the item can fetch the values
from the database again.
To demonstrate this, a
SqlCacheDependency object is created for the Authors table in
the
pubs database in Microsoft SQL Server using the CreateSqlCacheDependency
method. The pubs database is a sample database that ships with SQL Server 2000.
public SqlCacheDependency CreateSqlCacheDependency(string connStr)
{
// Make a dependency on the authors database table so that
// if it changes, the cached data will also be disposed of.
// Make sure we are enabled for notifications for ...