Cache application (asp.net 2.0 SQL data cache dependency [SqlCacheDependency]),
Asp.net 2.0 provides a new data caching function, that is, using the asynchronous notification function of SQL server2005 to implement caching.
1. Create a test database in sqlserver2005.
Run on SQL Server 2005
Alter database <DatabaseName> SET ENABLE_BROKER; the statement enables the listener service for the corresponding DATABASE to support the SqlDependency feature.
Add a database table for the employee.
1 CREATETABLE [dbo]. [employee] (
2 [id] [int] IDENTITY (1, 1) NOTNULL,
3 [name] [varchar] (50)
4)
5
2. Use vs2005 to create a new asp.net project.
Web. config is as follows:
1 <? Xmlversion = "1.0"?>
2 <configurationxmlns = "http://schemas.microsoft.com/.NetConfiguration/v2.0">
3 <appSettings/>
4 <connectionStrings>
5 <addname = "mySource" connectionString = "DataSource =. \ sql2005; InitialCatalog = test; PersistSecurityInfo = True; UserID = sa; Password = sasa "providerName =" System. data. sqlClient "> </add>
6 </connectionStrings>
7 <system. web>
8 <compilationdebug = "true"/>
9 <authenticationmode = "Windows"/>
10 </system. web>
11 </configuration>
12
3. Write the global. asax file and start listening for sql2005 notification events.
<% @ ApplicationLanguage = "C #" %>
<% @ ImportNamespace = "System. Data. SqlClient" %>
<Scriptrunat = "server">
VoidApplication_Start (objectsender, EventArgse)
{
StringconnStr = ConfigurationManager. ConnectionStrings ["mySource"]. ConnectionString;
SqlDependency. Start (connStr );
}
VoidApplication_End (objectsender, EventArgse)
{
StringconnStr = ConfigurationManager. ConnectionStrings ["mySource"]. ConnectionString;
SqlDependency. Stop (connStr );
}
</Script>
4. Write the data access code. Create a class for EmployeeData. The Code is as follows:
UsingSystem;
UsingSystem. Data;
UsingSystem. Data. SqlClient;
UsingSystem. Configuration;
UsingSystem. Data. Common;
UsingSystem. Web;
UsingSystem. Web. Caching;
UsingSystem. Web. Security;
UsingSystem. Web. UI;
UsingSystem. Web. UI. WebControls;
UsingSystem. Web. UI. WebControls. WebParts;
UsingSystem. Web. UI. HtmlControls;
/// <Summary>
/// Abstract description of EmployeeData
/// </Summary>
PublicclassEmployeeData
{
PublicEmployeeData ()
{
}
PrivateHttpContextcontext;
PublicDataSetGetCacheData ()
{
Context = HttpContext. Current;
DataSetcache = (DataSet) context. Cache ["employee"];
If (cache = null)
{
ReturnGetData ();
}
Else
{
Returncache;
}
}
PublicDataSetGetData ()
{
StringconnStr = ConfigurationManager. ConnectionStrings ["mySource"]. ConnectionString;
SqlConnectionconn = newSqlConnection (connStr );
SqlDataAdapteradp = newSqlDataAdapter ("selectid, namefromdbo. employee", conn );
SqlCacheDependencydep = newSqlCacheDependency (adp. SelectCommand );
DataSetds = newDataSet ();
Adp. Fill (ds );
Context. Cache. Add ("employee", ds, dep, Cache. NoAbsoluteExpiration, Cache. NoSlidingExpiration, CacheItemPriority. Default, newCacheItemRemovedCallback (this. DataDiff ));
Returnds;
}
PublicvoidDataDiff (stringkey, objectvalue, CacheItemRemovedReasonreason)
{
Console. WriteLine ("key:" + key );
GetData ();
}
}
Note that you cannot use the select * method to write the select statement. You must add the schema name before the table name, as shown in dbo. employee.
5. Compile the test page code.
<% @ PageLanguage = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
<! DOCTYPEhtmlPUBLIC "-/w3c // DTDXHTML1.0Transitional //" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<Htmlxmlns = "http://www.w3.org/1999/xhtml">
<Headrunat = "server">
<Title> No title page </title>
</Head>
<Body>
<Formid = "form1" runat = "server">
<Div>
<Asp: GridViewID = "GridView1" runat = "server">
</Asp: GridView>
</Div>
</Form>
</Body>
</Html>
6. Insert background code
UsingSystem;
UsingSystem. Data;
UsingSystem. Configuration;
UsingSystem. Web. Caching;
UsingSystem. Data. SqlClient;
UsingSystem. Web;
UsingSystem. Web. Security;
UsingSystem. Web. UI;
UsingSystem. Web. UI. WebControls;
UsingSystem. Web. UI. WebControls. WebParts;
UsingSystem. Web. UI. HtmlControls;
Publicpartialclass_Default: System. Web. UI. Page
{
ProtectedvoidPage_Load (objectsender, EventArgse)
{
EmployeeDataem = newEmployeeData ();
GridView1.DataSource = em. GetCacheData ();
GridView1.DataBind ();
}