F:">aspnet_regsql -E -S . -ed -d Northwind
为 SQL 缓存依赖项启用该数据库。
.
已完成。
F:">
|
F:">aspnet_regsql -C "data source=.;initial catalog=Test;integrated security=ssp
i" -ed
为 SQL 缓存依赖项启用该数据库。
.
已完成。
F:">
|
F:">aspnet_regsql -E -S . -ed -d Northwind -et -t Categories
为 SQL 缓存依赖项启用该数据库。
.
已完成。
为 SQL 缓存依赖项启用该表。
已完成。
F:">
|
F:">aspnet_regsql -C "data source=.;initial catalog=Test;integrated security=ssp
i" -et -t Categories
为 SQL 缓存依赖项启用该数据库。
.
已完成。
为 SQL 缓存依赖项启用该表。
已完成。
F:">
|
<
system.web
>
<
caching
>
<
sqlCacheDependency
enabled
=
"true"pollTime="5000">
<
databases
>
<
add
name
=
"Northwind"
connectionStringName
=
"NorthwindConnectionString"/>
</
databases
>
</
sqlCacheDependency
>
</
caching
>
</
system.web
>
|
<%
@
Page Language="C#" AutoEventWireup="true" CodeFile="
PollingSqlOutputCache.aspx
.cs"
Inherits="
PollingSqlOutputCache
"
%>
<%
@
OutputCache Duration="999999" VaryByParam="none" SqlDependency="Northwind:Categories" %>
<!
DOCTYPE
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html
xmlns="http://www.w3.org/1999/xhtml" >
<
head
runat="server">
<title>
对页面输出缓存使用基于轮询的
SQL
缓存依赖
</
title
>
</
head
>
<
body
>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False"
ReadOnly="True" SortExpression="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]">
</asp:SqlDataSource>
</div>
</form>
</
body
>
</
html
>
|
<%
@
Page Language="C#" AutoEventWireup="true" CodeFile="
PollingDataSourceCache.aspx
.cs"
Inherits="
PollingDataSourceCache
"
%>
<!
DOCTYPE
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html
xmlns="http://www.w3.org/1999/xhtml" >
<
head
runat="server">
<title>
对
DataSource
缓存使用基于轮询的
SQL
缓存依赖
</
title
>
</
head
>
<
body
>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False"
ReadOnly="True" SortExpression="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
EnableCaching="true"
SqlCacheDependency="Northwind:Categories"
SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]">
</asp:SqlDataSource>
</div>
</form>
</
body
>
</
html
>
|
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Data.SqlClient;
using
System.Web.Configuration;
using
System.Web.Caching;
public
partial class PollingDataCache : System.Web.UI.Page
{
private DataTable GetCategories()
{
string strConn =
WebConfigurationManager.
ConnectionStrings["NorthwindConnectionString"].
ConnectionString;
string strSql = "select CategoryID,CategoryName from Categories";
SqlConnection cn = new SqlConnection(strConn);
SqlDataAdapter da =
new SqlDataAdapter(strSql, cn);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = (DataTable)Cache["Category"];
if (dt == null)
{
dt = GetCategories();
SqlCacheDependency dep =
new SqlCacheDependency("Northwind", "Categories");
Cache.Insert("Category", dt, dep);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
|
SELECT
name,is_broker_enabled FROM sys.databases
|
ALTER
DATABASE Northwind SET ENABLE_BROKER
|
GRANT
SUBSCRIBE QUERY NOTIFICATIONS TO "COMPUTENAME"ASPNET"
|
<%
@
Application Language="C#" %>
<%
@
Import Namespace="System.Data.SqlClient" %>
<%
@
Import Namespace="System.Web.Configuration" %>
<
script
runat="server">
void Application_Start(object sender, EventArgs e)
{
string strConn =
WebConfigurationManager.
ConnectionStrings["TestConnectionString"].
ConnectionString;
SqlDependency.Start(strConn);
}
void Application_End(object sender, EventArgs e)
{
string strConn =
WebConfigurationManager.
ConnectionStrings["TestConnectionString"].
ConnectionString;
SqlDependency.Stop(strConn);
}
void Application_Error(object sender, EventArgs e)
{
}
void Session_Start(object sender, EventArgs e)
{
}
void Session_End(object sender, EventArgs e)
{
}
</
script
>
|