更新 : 2007 年 11 月
SqlDataSource コントロールは取得したデータをキャッシュできるため、多くのリソースを消費するクエリの再実行を回避して、アプリケーションのパフォーマンスを向上できます。キャッシュはデータが頻繁に変更されない場合に有効です。
さらに、SqlDataSource コントロールを System.Data.SqlClient プロバイダと共に使用する場合は、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 runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
EnableCaching="True"
CacheDuration="20"
SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees">
</asp:SqlDataSource>
<asp:GridView
id="GridView1"
runat="server"
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 runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
EnableCaching="True"
CacheDuration="20"
SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees">
</asp:SqlDataSource>
<asp:GridView
id="GridView1"
runat="server"
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 runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
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"
runat="server"
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 runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:gridview
id="GridView1"
runat="server"
datasourceid="SqlDataSource1" />
<asp:sqldatasource
id="SqlDataSource1"
runat="server"
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 runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:gridview
id="GridView1"
runat="server"
datasourceid="SqlDataSource1" />
<asp:sqldatasource
id="SqlDataSource1"
runat="server"
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 runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:gridview
id="GridView1"
runat="server"
datasourceid="SqlDataSource1" />
<asp:sqldatasource
id="SqlDataSource1"
runat="server"
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>
参照
概念
SqlDataSource Web サーバー コントロールの概要