快取含有 SqlDataSource 控制項的資料
更新:2007 年 11 月
SqlDataSource 控制項能夠快取擷取的資料,藉由避免重新執行耗用很多資源的查詢以增強應用程式效能。基本上,快取在不常變更資料的情況下很有用。
此外,當搭配 System.Data.SqlClient 提供者使用 SqlDataSource 控制項時,您可以利用 SqlCacheDependency 物件。這可以讓 SqlDataSource 控制項只有在 SelectCommand 傳回的資料在資料庫中被修改時,才會重新整理快取。
使用 SqlDataSource 控制項啟用快取
當 SqlDataSource 控制項的 DataSourceMode 屬性設定為 DataSet 時就能夠快取資料。預設不會啟用快取,但是您可以將 EnableCaching 屬性設定為 true 以啟用快取。
快取的資料會根據時間間隔重新整理。您可以將 CacheDuration 屬性設定為在重新整理快取之前等候的秒數。SqlDataSource 控制項會為每個 ConnectionString、SelectCommand 和 SelectParameters 值組合維護不同的快取項目。
您可以設定 CacheExpirationPolicy 屬性進一步控制 SqlDataSource 快取的行為。Absolute 的值會在超過 CacheDuration 值時強迫重新整理快取。將 CacheExpirationPolicy 屬性設定為 Sliding 會只有在最後一次存取快取項目後,超過 CacheDuration 值的情況下才重新整理快取。
下列程式碼範例,示範了設定為每 20 秒重新整理資料的 SqlDataSource 控制項:
<%@ Page language="VB" %>
<!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 >
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" >
<asp:SqlDataSource
id="SqlDataSource1"
DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
EnableCaching="True"
CacheDuration="20"
SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees">
</asp:SqlDataSource>
<asp:GridView
id="GridView1"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<columns>
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
<asp:BoundField HeaderText="Title" DataField="Title" />
</columns>
</asp:GridView>
</form>
</body>
</html>
<%@ Page language="C#" %>
<!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 >
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" >
<asp:SqlDataSource
id="SqlDataSource1"
DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
EnableCaching="True"
CacheDuration="20"
SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees">
</asp:SqlDataSource>
<asp:GridView
id="GridView1"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<columns>
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
<asp:BoundField HeaderText="Title" DataField="Title" />
</columns>
</asp:GridView>
</form>
</body>
</html>
<!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 >
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" >
<asp:SqlDataSource
id="SqlDataSource1"
DataSourceMode="DataSet"
ConnectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;"
EnableCaching="True"
CacheDuration="20"
SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees">
</asp:SqlDataSource>
<asp:GridView
id="GridView1"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
<asp:BoundField HeaderText="Title" DataField="Title" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
使用 SqlCacheDependency 物件
SqlDataSource 控制項根據 SqlCacheDependency 物件支援選擇性的到期原則。您可以使用 SqlCacheDependency 物件,確認只有在資料庫中的來源資料表被修改時才重新整理快取資料。若要使用 SqlCacheDependency 物件,Microsoft SQL Server 必須針對資料庫伺服器執行告知服務。
當您的資源來源是 SQL Server 7.0 (含) 以後版本時,可以將 SqlDataSource 控制項的 SqlCacheDependency 屬性設定為由連接字串和資料表識別項組成的字串,格式為 "ConnectionString:Table",以使用 SqlCacheDependency 物件輪詢資料庫的變更。如果 SqlCacheDependency 涉及一個以上的資料表,連接字串和資料表名稱組會以分號分隔,如同下列程式碼範例中所示:
"ConnectionString1:Table1;ConnectionString2:Table2"。
當您的資料來源是 Microsoft SQL Server 2005 時,可以有其他選項讓 SQL Server 告知應用程式發生變更而不是輪詢變更。您可以將 SqlCacheDependency 屬性設定為字串 "CommandNotification" 以便使用告知模型。如需 SqlCacheDependency 物件的詳細資訊,請參閱以 SqlCacheDependency 類別在 ASP.NET 中快取。
下列程式碼範例,示範了如何建立 Microsoft SQL Server 快取相依性以及設定 SqlDataSource 控制項的 SqlCacheDependency 屬性。在這個範例中,每 120 秒會輪詢一次資料庫。如果在這段時間內 Northwind Employees 資料表中的資料有變更,下一次輪詢資料庫時就會重新整理 SqlDataSource 控制項快取的資料,以及 GridView 控制項顯示的資料。
<%@ Page language="vb" %>
<!--
The page uses an example configuration that includes
connection strings and a defined SqlCacheDependecy.
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="MyNorthwind"
connectionString="Data Source="localhost";Integrated Security="SSPI";Initial Catalog="Northwind""
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<cache>
<sqlCacheDependency enabled="true">
<databases>
<add
name="Northwind"
connectionStringName="MyNorthwind"
pollTime="120000" />
</databases>
</sqlCacheDependency>
</cache>
</system.web>
</configuration>
-->
<!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 >
<title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" method="post" >
<asp:gridview
id="GridView1"
datasourceid="SqlDataSource1" />
<asp:sqldatasource
id="SqlDataSource1"
connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
selectcommand="SELECT EmployeeID,FirstName,Lastname FROM Employees"
enablecaching="True"
cacheduration="300"
cacheexpirationpolicy="Absolute"
sqlcachedependency="Northwind:Employees" />
</form>
</body>
</html>
<%@ Page language="c#" %>
<!--
The page uses an example configuration that includes
connection strings and a defined SqlCacheDependecy.
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="MyNorthwind"
connectionString="Data Source="localhost";Integrated Security="SSPI";Initial Catalog="Northwind""
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<cache>
<sqlCacheDependency enabled="true">
<databases>
<add
name="Northwind"
connectionStringName="MyNorthwind"
pollTime="120000" />
</databases>
</sqlCacheDependency>
</cache>
</system.web>
</configuration>
-->
<!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 >
<title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" method="post" >
<asp:gridview
id="GridView1"
datasourceid="SqlDataSource1" />
<asp:sqldatasource
id="SqlDataSource1"
connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
selectcommand="SELECT EmployeeID,FirstName,Lastname FROM Employees"
enablecaching="True"
cacheduration="300"
cacheexpirationpolicy="Absolute"
sqlcachedependency="Northwind:Employees" />
</form>
</body>
</html>
<%@ Page language="VJ#" %>
<!--
The page uses an example configuration that includes
connection strings and a defined SqlCacheDependecy.
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name = "NorthwindConnection" connectionString = "Data Source="localhost";Integrated Security="SSPI";Initial Catalog="Northwind"" />
</connectionStrings>
<system.web>
...
<cache>
<sqlCacheDependency enabled="true">
<databases>
<add
name="Northwind_Remote"
connectionStringName="NorthwindConnection"
pollTime="120000" />
</databases>
</sqlCacheDependency>
</cache>
</system.web>
</configuration>
-->
<!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 >
<title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" method="post" >
<asp:gridview
id="GridView1"
datasourceid="SqlDataSource1" />
<asp:sqldatasource
id="SqlDataSource1"
connectionstring="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind"
selectcommand="SELECT EmployeeID,FirstName,Lastname FROM Employees"
enablecaching="True"
cacheduration="300"
cacheexpirationpolicy="Absolute"
sqlcachedependency="Northwind_Remote:Employees" />
</form>
</body>
</html>