?? ex24-1vb.aspx
字號(hào):
<% @ Page Language="VB" Debug="true" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OleDb" %>
<html>
<head>
<title>第二十四例 數(shù)據(jù)庫(kù)的簡(jiǎn)單修改操作</title>
</head>
<Script Language="VB" Runat="Server">
Dim public myConnstring as string '聯(lián)結(jié)字符串為公有變量
Dim MyConnection as OleDbConnection
Dim public Shared convar as integer = 0 '控制進(jìn)行插入操作或修改操作的選擇(1為插入,2為修改)
public Sub Page_Load(sender As Object,e As EventArgs)
Div2.visible = false
If convar = 0 then
Div1.visible = false
End If
End Sub '在初始化頁(yè)面時(shí),Div1,Div2均為不可見
Function Myconn()
myConnstring ="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=f:/Asp_net/書稿/" + Textbox1.text
MyConnection = new OleDbConnection(myConnstring)
End Function '定義設(shè)置聯(lián)結(jié)的函數(shù)
public Sub TextChange1(sender As Object,e As EventArgs)
Myconn()
ConData("Select * from Student Order by ID",0)
End Sub '當(dāng)輸入文本框內(nèi)容改變時(shí),查詢指定數(shù)據(jù)庫(kù)的數(shù)據(jù)
Function ConData(constr as String,Show as integer) as boolean
try
Dim MyCommand as OleDbCommand = new OleDbCommand(constr,MyConnection)
MyConnection.Open()
Dim MyDataReader as OleDbDataReader = MyCommand.ExecuteReader()
If Show=0 then
DataGrid1.DataSource=myDataReader
DataGrid1.DataBind()
End If
myDataReader.Close()
MyConnection.Close()
Info.Text="訪問數(shù)據(jù)庫(kù)成功"
Textbox1.Visible=false
Div1.visible = true
Return true
catch ee As Exception
MyConnection.Close()
Info.Text="發(fā)生錯(cuò)誤:"+ee.Message +" 或者,請(qǐng)確認(rèn)您輸入的數(shù)據(jù)庫(kù)名稱無誤:"
Textbox1.Visible=true
return false
finally
End try
End Function '訪問數(shù)據(jù)庫(kù)的函數(shù),當(dāng)訪問成功返回true,否則返回false
public Sub btn_click1(sender As Object,e As EventArgs)
Myconn()
ConData("delete * from Student where ID=" + input1.value,1)
ConData("Select * from Student Order by ID",0)
End Sub '刪除操作
public Sub btn_click2(sender As Object,e As EventArgs)
Convar = 2
Myconn()
Dim Upstr as String = "Select * from student where ID = " + Input2.value
if ConData(Upstr,1) then
info.text="修改第" + input2.value + "條記錄的操作!"
Div2.visible = true
Div1.visible = false
Else
Div2.visible = false
Div1.visible = true
End if
End Sub '修改操作
public Sub btn_click3(sender As Object,e As EventArgs)
Convar = 1
Myconn()
info.text="插入操作!"
Div2.visible = true
Div1.visible = false
End Sub '插入操作
public Sub btn_click4(sender As Object,e As EventArgs)
Myconn()
info.text=convar.toString
if Convar = 2 then
Dim Upstr as String = "UPDATE Student SET 學(xué)號(hào)=" + Input3.value + ", 姓名='" + Input4.value + "', 性別= '" + Input5.value + "', 成績(jī)= " + Input6.value + " where ID=" + Input2.value
if ConData(Upstr,1) then
ConData("Select * from Student Order by ID",0)
info.text="修改成功!"
Else
Div2.visible=true
End If
else if Convar = 1 then
Dim Insstr as String = "Insert Into Student(學(xué)號(hào),姓名,性別,成績(jī)) values (" + Input3.value + ", '"+Input4.value+"' , '" + Input5.value + "', " + Input6.value + ")"
if ConData(Insstr,1) then
ConData("Select * from Student Order by ID",0)
info.text="插入成功!"
Else
Div2.visible=true
End If
else
Div2.visible=true
End if
End Sub '輸入數(shù)據(jù)的提交按鈕
public Sub btn_click5(sender As Object,e As EventArgs)
Div1.visible = true
Div2.visible = false
End Sub '取消提交
</Script>
<body>
<h3><font face="Verdana">第二十四例 數(shù)據(jù)庫(kù)的簡(jiǎn)單修改操作</font></h3>
<form runat="server">
<asp:Label id="Info" runat=server text="請(qǐng)輸入數(shù)據(jù)庫(kù)文件名稱:"/>
<asp:TextBox id="TextBox1" AutoPostBack=true OnTextChanged=TextChange1 runat=server /><br>
<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 visible=false><asp:Label id="label1" runat="server" Text="請(qǐng)輸入您要?jiǎng)h除的ID號(hào):" />
<Input id=Input1 Size=3 runat=server OnServerChange="Btn_Click1"/>
<asp:button id="Btn1" runat=server OnClick=Btn_Click1 text="確定" /><br>請(qǐng)輸入您要修改的ID號(hào):
<Input id=Input2 Size=3 runat=server OnServerChange="Btn_Click2"/>
<asp:button id="Btn2" runat=server OnClick=Btn_Click2 text="確定" /><br>如果你要插入新記錄請(qǐng)點(diǎn)這里-->
<asp:button id="Btn3" runat=server OnClick=Btn_Click3 text="插入" /></div><br>
<DIV style="BACKGROUND-COLOR: aqua" id="Div2" runat=server>請(qǐng)輸入您的修改值<br>學(xué)號(hào):
<Input id=Input3 Size=5 runat=server/>姓名:
<Input id=Input4 Size=5 runat=server/>性別:
<Input id=Input5 Size=2 runat=server/>成績(jī):
<Input id=Input6 Size=3 runat=server/>
<asp:Button id="Btn4" runat=server OnClick="Btn_Click4" text="提交"/>
<asp:Button runat=server OnClick="Btn_Click5" text="取消"/>
</Div>
</form>
</body>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -