⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex25-2vb.aspx

📁 这是该书上的所有源代码,不错
💻 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")

	If InStr(SqlStr,"Student") <> 0 then
		MyComm.SelectCommand.CommandText = "select count(*) as co from student"
	Else
		MyComm.SelectCommand.CommandText = "select count(*) as co from Book"
	End If
	Mycomm.Fill(MyDataSet,"Count")
	Myconnection.Close()
	DataGrid1.DataSource = MyDataSet.Tables("Table1").DefaultView
	DataGrid1.DataBind()
	Dim CountStr as String = MyDataset.tables("Count").Rows(0)(0).toString
	Label1.text = "本数据表中共有<font size=+2 color=red>" + CountStr + "</font>条记录"
	'Label1.text = InStr(SqlStr,"Student")
	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 + -