?? ex26-1vb.aspx
字號:
<% @ 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>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -