<% @ Page Language="VB" Debug="true" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OleDb" %>
<html>
<head>
<title>第二十六例 使用DataSet更新数据库</title>
<Script Language="VB" Runat="Server">

	dim public selall as String = "Select * from Student order by id"
	public Sub Page_Load(sender as Object,e as EventArgs)
	if not page.ispostback then
	Select1(SelAll,1)
	div2.visible = false
	end if
	End Sub
	'第一次初始化页面时执行If中语句

	public Sub btn1click(sender as Object,e as EventArgs)
	Select1("Select * from Student where id=" + DropDownList1.SelectedItem.value,2)
	Select1(SelAll,1)
	End Sub
	'删除操作，选择Conval为2

	public Sub btn2click(sender as Object,e as EventArgs)
	If DropDownList2.SelectedItem.value = "ID" then
	label2.text="<font size=+3 color=Red>不能修改ID！</font>"
	else
	label2.text=""
	Select1("Select * from Student where id=" + DropDownList1.SelectedItem.value,3)
	Select1(SelAll,0)
	div2.visible = true
	div1.visible = false
	end if
	End Sub
	'修改操作，设置Conval为3

	public Sub btn3click(sender as Object,e as EventArgs)
	Select1("Select * from Student where id=" + DropDownList1.SelectedItem.value,4)
	Select1(SelAll,0)
	div1.visible = true
	div2.visible = false
	End Sub
	'确定修改操作

	public Sub btn4click(sender as Object,e as EventArgs)
	div1.visible = true
	div2.visible = false
	End Sub
	'取消修改操作

	Function Select1(SqlStr as String, Convar as integer)
	'控制联结的函数,Convar变量控制执行函数中的指定段代码
	Dim MyConnString As string = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("aspnet.mdb")
	Dim MyConnection as OleDbConnection = new OleDbConnection(MyConnString)
	Dim myDataAdapter as OleDbDataAdapter = new OleDbDataAdapter(SqlStr,MyConnection)
	Dim MyDataSet as DataSet = new DataSet()
	myDataAdapter.Fill(MyDataSet,"Table1")
	if Convar=1 then
	myDataAdapter.SelectCommand.CommandText = "select ID from student"
	myDataAdapter.Fill(MyDataSet,"ID1")
	DropDownList1.DataSource = MyDataSet.tables("ID1").DefaultView
        DropDownList1.DataTextField="ID"
        DropDownList1.DataValueField="ID"
	DropDownList1.DataBind()
	'为DropDownList1绑定数据，为数据表的ID列的数据
	DropDownList2.DataSource = MyDataSet.tables("Table1").Columns
	DropDownList2.DataBind()
	'为DropDownList2绑定数据表各列的列名
	Else if Convar=2 then
	dim mybuilder as OleDbCommandBuilder = new OleDbCommandBuilder(myDataAdapter)
	MyDataSet.Tables("table1").Rows(0).delete()
	myDataAdapter.Update(MyDataSet,"table1")
	'删除指定ID的记录
	Else if Convar=3 then
	textbox1.text = MyDataSet.Tables("table1").Rows(0)(DropDownList2.SelectedItem.value)
	label1.text="请输入记录（ID=<font size=+2 color=red>" + DropDownList1.SelectedItem.value +"</font>）的修改值  <font size=+2 color=red>"+DropDownList2.SelectedItem.value+"：</font>"
	'修改记录时，将数据表中的数据都到控件中
	Else if Convar=4 then
	dim mybuilder as OleDbCommandBuilder = new OleDbCommandBuilder(myDataAdapter)
	MyDataSet.Tables("table1").Rows(0)(DropDownList2.SelectedItem.value) = textbox1.text
	myDataAdapter.Update(MyDataSet,"table1")
	'执行修改操作，将修改后的值存入数据表
	End if
	DataGrid1.DataSource = MyDataSet.Tables("Table1").DefaultView
	DataGrid1.DataBind()
	'DataGrid控件的绑定
	Myconnection.Close()
	'关闭数据库
	End Function



</script>

</head>
<body>
	<h3><font face="Verdana">第二十六例 使用DataSet更新数据库</font></h3>
	<form runat=server>数据表中数据显示如下：
<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" /><br>
	<div id=div1 runat=server>选择要修改或删除记录ID：
	<asp:DropDownList id=DropDownList1 AutoPostBack=true runat=server />
	<asp:button id=button1 runat=server onClick=btn1click text="删除" /><br>选择修改记录中的哪一列：
	<asp:DropDownList id=DropDownList2 runat=server />
	<asp:Button id=button2 runat=server onClick=btn2click text="修改" /></div>
	<div id=div2 runat=server style="BACKGROUND-COLOR: aqua">
	<asp:label runat=server id=label1/>
	<asp:textBox id=textbox1 runat=server width=50/>
	<asp:Button id=button3 runat=server onClick=btn3click text="确定" />
	<asp:Button id=button4 runat=server onClick=btn4click text="取消" />
	</div>	
	<asp:label runat=server id=label2/>
	</form>
</body>
</html>





