?? ex25-1vb.aspx
字號:
<% @ Page Language="VB" Debug="true" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OleDb" %>
<html>
<head>
<title>第二十五例 通過DataAdapter和DataSet訪問數據庫</title>
<Script Language="VB" Runat="Server">
public Sub Page_Load(sender as Object,e as EventArgs)
if RadioButtonList1.SelectedIndex = -1 then
RadioButtonList1.SelectedIndex = 0
End if
End Sub
'第一次裝載頁面時,控制RadioButtonList有一個選擇
public Sub RadioChange(sender as Object,e as EventArgs)
if RadioButtonList1.SelectedIndex = 0 then
Select1("Select * from Student")
Else if RadioButtonList1.SelectedIndex = 1 then
Select1("Select * from Book")
End if
End Sub
'RadioButtonList選擇項改變時觸發該事件
Function Select1(SqlStr as String)
Dim MyConnString As string = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("aspnet.mdb")
Dim MyConnection as OleDbConnection = new OleDbConnection(MyConnString)
'建立與數據庫的聯結
Dim MyComm as OleDbDataAdapter = new OleDbDataAdapter(SqlStr,MyConnection)
Dim MyDataSet as DataSet = new DataSet()
'定義MyDataSet
MyComm.Fill(MyDataSet,"Table1")
'將查詢結果存入Dataset,命名表為Table1
Myconnection.Close()
'關閉與數據庫的聯結
DataGrid1.DataSource = MyDataSet.Tables("Table1").DefaultView
DataGrid1.DataBind()
'數據綁定
Label1.text = "本數據表中共有<font size=+2 color=red>" + MyDataset.tables("Table1").DefaultView.Count.toString + "</font>條記錄"
End Function
'訪問數據庫函數
</script>
</head>
<body>
<h3><font face="Verdana">第二十五例 通過DataAdapter和DataSet訪問數據庫</font></h3>
<form runat=server>
<asp:Label id=info runat=server text="" />選擇您要查詢的數據表:
<asp:RadioButtonList id=RadioButtonList1 runat="server" AutoPostBack=true OnSelectedIndexChanged="RadioChange">
<asp:ListItem>Student</asp:ListItem>
<asp:ListItem>Book</asp:ListItem>
</asp:RadioButtonList>
<ASP:DataGrid id="DataGrid1" runat="server" BorderColor="black" BorderWidth="1" GridLines="Both" CellPadding="3" CellSpacing="0" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd" AlternatingItemStyle-BackColor="#eeeeee"/>
<asp:Label id=Label1 runat=server text="" />
</form>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -