Public Class ViewPhoneRecords
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim da As OleDb.OleDbDataAdapter
Dim ds As New DataSet
Dim sqlquery As New OleDb.OleDbCommand
Dim con1 As New OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Database.accdb")
Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click
Dim sqlupdate As String
' Here we use the UPDATE Statement to update the information. To be sure we are
' updating the right record we also use the WHERE clause to be sureno information
' is added or changed in the other records
sqlupdate = "UPDATE PhoneRecords SET Forename=@Forename, Surname=@Surname, Address=@Address, PhoneModel=@PhoneModel, PhoneNumber=@PhoneNumber, Postcode=@Postcode WHERE IDNum='" & IDTextBox.Text & "'"
Dim cmd As New OleDbCommand(sqlupdate, con1)
' This assigns the values for our columns in the DataBase.
' To ensure the correct values are written to the correct column
cmd.Parameters.Add(New OleDbParameter("@Forename", ForenameTextBox1.Text))
cmd.Parameters.Add(New OleDbParameter("@Surname", SurnameTextBox1.Text))
cmd.Parameters.Add(New OleDbParameter("@Address", AddressTextBox1.Text))
cmd.Parameters.Add(New OleDbParameter("@PhoneModel", PhoneModelTextBox1.Text))
cmd.Parameters.Add(New OleDbParameter("@PhoneNumber", PhoneNumberTextBox1.Text))
cmd.Parameters.Add(New OleDbParameter("@Postcode", PostcodeTextBox1.Text))
con1.Open()
cmd.ExecuteNonQuery()
MsgBox("Row(s) Inserted !! ") 'Displays message box informing the user that the database has been added to
con1.Close() 'Connection closed
Me.Refresh()
End Sub
这应该更新数据网格视图中的选定记录。但是,当我点击“保存更改”时,按钮,给出错误; "没有给出一个或多个参数的值。"知道如何解决这个问题吗?
答案 0 :(得分:0)
使用cmd.Parameters.AddWithValue
代替cmd.Parameters.Add