?
本文檔使用 php中文網(wǎng)手冊(cè) 發(fā)布
ADO.NET 也是 .NET 框架的組成部分。ADO.NET 用于處理數(shù)據(jù)訪問。通過 ADO.NET,您可以操作數(shù)據(jù)庫。
數(shù)據(jù)庫連接 - 綁定到 DataList 控件
<%@?Import?Namespace="System.Data.OleDb"?%> <script??runat="server"> sub?Page_Load dim?dbconn,sql,dbcomm,dbread dbconn=New?OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data?source="?&?server.mappath("/db/northwind.mdb")) dbconn.Open() sql="SELECT?*?FROM?customers" dbcomm=New?OleDbCommand(sql,dbconn) dbread=dbcomm.ExecuteReader() customers.DataSource=dbread customers.DataBind() dbread.Close() dbconn.Close() end?sub </script> <!DOCTYPE?html> <html> <body> <form?runat="server"> <asp:DataList id="customers" runat="server" cellpadding="2" cellspacing="2" borderstyle="inset" backcolor="#e8e8e8" width="100%" headerstyle-font-name="Verdana" headerstyle-font-size="12pt" headerstyle-horizontalalign="center" headerstyle-font-bold="True" itemstyle-backcolor="#778899" itemstyle-forecolor="#ffffff" footerstyle-font-size="9pt" footerstyle-font-italic="True"> <HeaderTemplate> Customers?Table </HeaderTemplate> <ItemTemplate> <%#Container.DataItem("companyname")%>??in <%#Container.DataItem("address")%>,?<%#Container.DataItem("city")%> </ItemTemplate> <FooterTemplate> Source:?Northwind?Database </FooterTemplate> </asp:DataList> </form> </body> </html>
數(shù)據(jù)庫連接 - 綁定到 Repeater 控件
<%@?Import?Namespace="System.Data.OleDb"?%> <script??runat="server"> sub?Page_Load dim?dbconn,sql,dbcomm,dbread dbconn=New?OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data?source="?&?server.mappath("/db/northwind.mdb")) dbconn.Open() sql="SELECT?*?FROM?customers" dbcomm=New?OleDbCommand(sql,dbconn) dbread=dbcomm.ExecuteReader() customers.DataSource=dbread customers.DataBind() dbread.Close() dbconn.Close() end?sub </script> <!DOCTYPE?html> <html> <body> <form?runat="server"> <asp:Repeater?id="customers"?runat="server"> <HeaderTemplate> <table?border="1"?width="100%"> <tr?bgcolor="#b0c4de"> <th>Companyname</th> <th>Contactname</th> <th>Address</th> <th>City</th> </tr> </HeaderTemplate> <ItemTemplate> <tr?bgcolor="#f0f0f0"> <td><%#Container.DataItem("companyname")%>?</td> <td><%#Container.DataItem("contactname")%>?</td> <td><%#Container.DataItem("address")%>?</td> <td><%#Container.DataItem("city")%>?</td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </form> </body> </html>
在我們的實(shí)例中,我們將使用 Northwind 數(shù)據(jù)庫。
首先,導(dǎo)入 "System.Data.OleDb" 命名空間。我們需要這個(gè)命名空間來操作 Microsoft Access 和其他 OLE DB 數(shù)據(jù)庫提供商。我們將在 Page_Load 子例程中創(chuàng)建這個(gè)數(shù)據(jù)庫的連接。我們創(chuàng)建一個(gè) dbconn 變量,并為其賦值一個(gè)新的 OleDbConnection 類,這個(gè)類帶有指示 OLE DB 提供商和數(shù)據(jù)庫位置的連接字符串。然后我們打開數(shù)據(jù)庫連接:
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
sub Page_Load
dim dbconn
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
data source=" & server.mappath("northwind.mdb"))
dbconn.Open()
end sub
</script>
注釋:這個(gè)連接字符串必須是沒有折行的連續(xù)字符串!
為了指定需從數(shù)據(jù)庫取回的記錄,我們將創(chuàng)建一個(gè) dbcomm 變量,并為其賦值一個(gè)新的 OleDbCommand 類。這個(gè) OleDbCommand 類用于發(fā)出針對(duì)數(shù)據(jù)庫表的 SQL 查詢:
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
data source=" & server.mappath("northwind.mdb"))
dbconn.Open()
sql="SELECT * FROM customers"
dbcomm=New OleDbCommand(sql,dbconn)
end sub
</script>
OleDbDataReader 類用于從數(shù)據(jù)源中讀取記錄流。DataReader 是通過調(diào)用 OleDbCommand 對(duì)象的 ExecuteReader 方法來創(chuàng)建的:
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
data source=" & server.mappath("northwind.mdb"))
dbconn.Open()
sql="SELECT * FROM customers"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
end sub
</script>
然后,我們綁定 DataReader 到 Repeater 控件:
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
data source=" & server.mappath("northwind.mdb"))
dbconn.Open()
sql="SELECT * FROM customers"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
customers.DataSource=dbread
customers.DataBind()
dbread.Close()
dbconn.Close()
end sub
</script>
<html>
<body>
<form runat="server">
<asp:Repeater id="customers" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Companyname</th>
<th>Contactname</th>
<th>Address</th>
<th>City</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Container.DataItem("companyname")%></td>
<td><%#Container.DataItem("contactname")%></td>
<td><%#Container.DataItem("address")%></td>
<td><%#Container.DataItem("city")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
如果不再需要訪問數(shù)據(jù)庫,請(qǐng)記得關(guān)閉 DataReader 和數(shù)據(jù)庫連接:
dbread.Close()
dbconn.Close()