The SqlCacheDependency Class

SqlCacheDependency is a new class in ASP.NET 2.0. It is found in the System.Web.Caching namespace and is used to create cache dependencies on Microsoft SQL Server 7, 2000, and "Yukon" databases.

The class inherits from CacheDependency , which has been unsealed for ASP.NET 2.0. Interaction with SqlCacheDependency can be achieved only through the two public constructors:

 SqlCacheDependency (  databaseEntry  As String,  tableName  As String) SqlCacheDependency (  sqlCmd  As SqlCommand) 

Creating SQL Server 7 and 2000 Dependencies

The following constructor is used to create database dependencies on Microsoft SQL Server 7 and 2000 databases:

 SqlCacheDependency (  databaseEntry  As String,  tableName  As String) 

It requires that the web.config <sqlCacheDependency /> section is defined; the databaseEntry parameter matches the name value added in <sqlCacheDependency /> . The tableName parameter is used to name a table in the database to monitor for changes. The named table must be enabled for change notifications. If the table name contains special characters or spaces, use brackets, as shown below:

 Dim dp As New SqlCacheDependency("MyDatabase", "[  Some Table  ]") 

Creating SQL Server "Yukon" Dependencies

The following constructor can be used to create dependencies only for SQL Server "Yukon":

 SqlCacheDependency (  sqlCmd  As SqlCommand) 

Although not discussed in this book, a dependency created on a "Yukon" database requires only the SqlCommand used when accessing the database (see Listing 11.4).

Listing 11.4 Creating a SqlCacheDependency on a "Yukon" Database
 <%@ Page Language="VB" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <script runat="server">   Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)     Dim connection As New SqlConnection( _         "server=.;database=Northwind;uid=sa;pwd=00password")     Dim command As New SqlCommand("SELECT * FROM Products", _                                   connection)     ' Create the SqlCacheDependency on a "Yukon" database     '     Dim dp As New SqlCacheDependency(command)     ' Set up page output caching     '     Response.Cache.SetExpires(DateTime.Now.AddSeconds(60))     Response.Cache.SetCacheability(HttpCacheability.Public)     Response.Cache.SetValidUntilExpires(True)     ' Make this page dependent on the SqlCacheDependency     '     Response.AddCacheDependency(dp)     connection.Open()     DataGrid1.DataSource = command.ExecuteReader()     DataGrid1.DataBind()     connection.Close()   End Sub </script> <h1>Last update: <%=DateTime.Now.ToString("r")%></h1> <hr> <asp:DataGrid runat="server" id="DataGrid1" /> 

As mentioned above, SqlCacheDependency inherits from CacheDependency , which was a sealed class in ASP.NET 1.0. In ASP.NET 2.0 Microsoft has unsealed CacheDependency , added a default public constructor, and changed some of the internal plumbing of the class so that you can inherit from it and create your own cache dependencies.

Let's take a look at the changes to the CacheDependency class.



A First Look at ASP. NET v. 2.0 2003
A First Look at ASP. NET v. 2.0 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 90

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net