用
INSERT插到数据库里。
第二张,点击button是,把每个要存的值付给参数,然后通过函数传值,调用SQL的存储过程,也就是通常我们所说的SP。然后在数据库里的SP里写存储语句就可以。
第二张方法是工作时候我才知道的,上大学的时候,一直只会第一种方法。
用insert语句插入,如下例:Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码()
If Not Page.IsPostBack Then
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0 Data Source=" + Server.MapPath("Examination.mdb")
Dim myConn As OleDb.OleDbConnection = New OleDb.OleDbConnection(strConn)
'打开链接
myConn.Open()
Dim da As New OleDb.OleDbDataAdapter("Select * From ExamineeInfo", myConn)
Dim ds As New DataSet
da.Fill(ds, "ExamineeInfo")
dg.DataSource = ds.Tables("ExamineeInfo")
dg.DataBind()
myConn.Close()
bind()
End If
End Sub
'使用一个Button控件
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If Page.IsValid Then
Dim SqlStr, ConStr, AddName, AddID, AddSex As String
Dim AddEnglishScore, AddComputerScore As Integer
ConStr = "Provider=Microsoft.Jet.OLEDB.4.0"
ConStr += "Data Source=" &Server.MapPath("Examination.mdb")
Dim myConn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConStr)
myConn.Open()
AddName = txtName.Text
AddID = txtID.Text
AddSex = txtSex.SelectedItem.Value
SqlStr = "select * from ExamineeInfo where id='" &txtID.Text &"'"
Dim myCommand As OleDb.OleDbCommand = New OleDb.OleDbCommand(SqlStr, myConn)
'用reader对象来查询用户名是否已经存在
Dim reader As OleDb.OleDbDataReader
reader = myCommand.ExecuteReader()
If reader.Read() Then
message.Text = "考生信息表中已经有此考生ID号!"
reader.Close()
Else
reader.Close()
SqlStr = "Insert Into ExamineeInfo "
SqlStr += "(ID,Name,Sex,EnglishScore,ComputerScore) "
SqlStr += "Values ('" &AddID &"', '"
SqlStr += AddName &"', '" &AddSex &"','0','0')"
myCommand = New OleDb.OleDbCommand(SqlStr, myConn)
myCommand.ExecuteNonQuery()
Response.Redirect("ExamineeInfo.aspx")
Exit Sub
End If
myConn.Close()
End If
End Sub
注意:我使用的是ACCESS数据库,如果使用SQL数据库时,把连接数据库的语句改一下就行了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)