egories和products表的情况。它在一个DropDownList中显示所有的产品种类并在一个GridView控件中显示属于一个特定种类的产品。首先,让我们使用Visual Studio 2005创建一个新的网站,并命名为Caching。然后,把一个Web页面TimeBasedCaching.aspx添加到其中。最后,按如下所示修改TimeBasedCaching.aspx文件中的代码:
<%@ Page Language="C#" %>
<html>
<head>
<title>SqlDataSource控件缓冲与参数分析试验</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList DataValueField="CategoryID"
DataTextField="CategoryName"
DataSourceID="CategoriesDataSource"
ID="DropDownList1" Runat="server" AutoPostBack="True">
</asp:DropDownList>
<br/><br/>
<asp:GridView ID="GridView1" Runat="server"
DataSourceID="ProductsDataSource"
DataKeyNames="ProductID" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="ProductID" DataField="ProductID"
SortExpression="ProductID" />
<asp:BoundField HeaderText="Timestamp" DataField="Timestamp"
SortExpression="Timestamp" />
<asp:BoundField HeaderText="ProductName"
DataField="ProductName"
SortExpression="ProductName" />
<asp:BoundField HeaderText="QuantityPerUnit"
DataField="QuantityPerUnit"
SortExpression="QuantityPerUnit" />
<asp:BoundField HeaderText="UnitPrice" DataField="UnitPrice"
SortExpression="UnitPrice" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="ProductsDataSource" Runat="server"
SelectCommand="SELECT DatePart(second, GetDate())
As Timestamp, *
FROM [Products] where CategoryID = @CategoryID"
ConnectionString="<%$ ConnectionStrings:Northwind %>"
EnableCaching="True" CacheDuration="10">
<SelectParameters>
<asp:ControlParameter Name="CategoryID"
ControlID="DropDownList1"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="CategoriesDataSource" Runat="server"
SelectCommand="SELECT * FROM [Categories]"
ConnectionString="<%$
ConnectionStrings:Northwind %>"
EnableCaching="True" CacheDuration="10"/>
</form>
</body>
</html>
在上面的代码中,到数据库的连接字符串是从web.config文件中检索的。这个web.config文件包含下列connectionStrings元素:
<connectionStrings>
<add name="Pubs"
connectionString="server=localhost;database=Pubs;
trusted_connection=true"/>
<add name="Northwind"
connectionString="server=localhost;database=Pubs;
trusted_connection=true"/>
</connectionStrings>
现在,既然要求的连接字符串已 |