Chapter 2:
使用AccessDataSource控件
示例:
<asp:AccessDataSource ID="NorthwindProductsAccDataSource" runat="server"
DataFile="~/App_Data/Northwind.mdb"
SelectCommand="SELECT * FROM [Products]"></asp:AccessDataSource>
<asp:GridView ID="NorthwindProductsGridVIew" runat="server"
AutoGenerateColumns="False"
DataKeyNames="ProductID"
DataSourceID="NorthwindProductsAccDataSource">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
InsertVisible="False"
ReadOnly="True"
SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName"
SortExpression="ProductName" />
<asp:BoundField DataField="SupplierID" HeaderText="SupplierID"
SortExpression="SupplierID" />
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
SortExpression="CategoryID" />
<asp:BoundField DataField="QuantityPerUnit"
HeaderText="QuantityPerUnit"
SortExpression="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
SortExpression="UnitPrice" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock"
SortExpression="UnitsInStock" />
<asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder"
SortExpression="UnitsOnOrder" />
<asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel"
SortExpression="ReorderLevel" />
<asp:CheckBoxField DataField="Discontinued"
HeaderText="Discontinued"
SortExpression="Discontinued" />
</Columns>
</asp:GridView>
或者用事件方法取得数量连接:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/Northwind.mdb"
SelectCommand="SELECT * FROM [Products]"
OnSelected="AccessDataSource1_Selected" >
</asp:AccessDataSource>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
Sub AccessDataSource1_Selected(ByVal sender As Object, ByVal e As
SqlDataSourceStatusEventArgs)
If (Not e.Exception Is Nothing) Then
If TypeOf e.Exception Is System.Data.OleDb.OleDbException Then
Message.Text = "There was a problem opening a connection to the
database. Please contact the system administrator for this site."
' Optionally set GridView1.Visible = false
e.ExceptionHandled = True
End If
End If
End Sub
</script>
<html>