问题属于这个按钮,我在这里得到错误:
objcommand.ExecuteNonQuery()
按下此按钮后,它应该更新我的mysql数据库中的所有记录。这是我的代码(确切的代码适用于另一个表):
Private Sub btnModify1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify1.Click
Using objconnection = New MySqlConnection("Server=localhost;database=ba-solutions;user id=root;password=")
objconnection.Open()
If Len(cbxCompanyName.Text) < 1 Then
MsgBox("Please select a company name")
Return
End If
If Len(txtPosition.Text) < 1 Then
MsgBox("Please enter the postion of the shareholder in the company")
Return
End If
If Len(txtNI.Text) <> 8 Then
MsgBox("The National Insurance Number must be 8 characters")
Return
End If
If Len(txtAddressLine.Text) < 1 Then
MsgBox("Enter a First Line of Address")
Return
End If
If Len(txtCity.Text) < 1 Then
MsgBox("Enter a City Name")
Return
End If
If Len(txtPostcode.Text) < 1 Then
MsgBox("Enter a Postcode")
Return
End If
If Len(txtEmail.Text) < 1 Then
MsgBox("Enter an Email Address")
Return
End If
If Len(txtPhoneNumber.Text) <> 11 Then
MsgBox("The Phone Number must be 11 numbers ")
Return
End If
'all validations for relevant textboxes
Dim companyname As String
Dim position As String
Dim ninumber As String
Dim dob As String
Dim forename As String
Dim surname As String
Dim addressline1 As String
Dim city As String
Dim postcode As String
Dim phonenumber As String
Dim email As String
postcode = txtPostcode.Text
companyname = cbxCompanyName.Text
position = txtPosition.Text
ninumber = txtNI.Text
dob = DTP1.Value
forename = txtForename.Text
surname = txtSurname.Text
addressline1 = txtAddressLine.Text
city = txtCity.Text
phonenumber = txtPhoneNumber.Text
email = txtEmail.Text
sqlstring = "UPDATE shareholder_details SET position=@position, ni=@ninumber, dob=@dob, forename=@forename, surname=@surname, addressline1=@addressline, city=@city, postcode=@postcode, phonenumber=@phone, email=@email where companyname= @company "
objcommand = New MySqlCommand(sqlstring, objconnection)
objcommand.Parameters.AddWithValue("@company", companyname)
objcommand.Parameters.AddWithValue("@postion", position)
objcommand.Parameters.AddWithValue("@ni", ninumber)
objcommand.Parameters.AddWithValue("@dob", dob)
objcommand.Parameters.AddWithValue("@forename", forename)
objcommand.Parameters.AddWithValue("@surname", surname)
objcommand.Parameters.AddWithValue("@addressline", addressline1)
objcommand.Parameters.AddWithValue("@city", city)
objcommand.Parameters.AddWithValue("@postcode", postcode)
objcommand.Parameters.AddWithValue("@phone", phonenumber)
objcommand.Parameters.AddWithValue("@email", email)
objcommand.ExecuteNonQuery()
btnRefresh.PerformClick()
Dim check As Integer = objcommand.ExecuteReader.RecordsAffected
If check < 1 Then
MessageBox.Show("Record was not updated, please try again", "Update unsucessfull",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("Record updated sucessfully", "Update sucessfull",
MessageBoxButtons.OK, MessageBoxIcon.Information)
objconnection.Close()
End If
End Using
End Sub
答案 0 :(得分:1)
objcommand.Parameters.AddWithValue("@postion", position)
但这可能应该是
objcommand.Parameters.AddWithValue("@position", position)
其他问题可能是,
在SQL语句中,您有ni=@ninumber
,
但是在主变量中使用objcommand.Parameters.AddWithValue("@ni", ninumber)
并且在您的SQL语句中,与使用其他主机变量
相比,它看起来与companyname= @company
不同
并且您可以尝试使用与在SQL语句中使用的objcommand.Parameters.AddWithValue("@host_variable", variable)
相同的顺序
答案 1 :(得分:0)
我使用的解决方案,它确实有效。 此错误主要是由MISSING或错误拼写的参数声明引起的。例如。 @FirstName错误地拼写为@FirtName。
确保在sql查询中声明的所有参数都在AddwithValue参数声明中声明。 (它有助于计算查询与Addwithvalues的比较)。
最佳解决方案是让visual studio提供有关缺失参数的信息。使用Try-Catch块。在catch块中使用Messagebox.show(ex.Innerexception.Message)而不是Messagebox.show(ex.message)。这将显示缺少的完全参数。例如。以下
尝试
conCommand.Parameters.Addwithvalue(“@ FirstName”,txtFirstName.text)
conCommand.Parameters.Addwithvalue(“@ MiddleName”,txtMiddleName.text)
conCommand.Parameters.Addwithvalue(“@ LastName”,txtLastName.text)
conCommand.Parameters.Addwithvalue(“@ PhoneNo”,txtPhoneno.text)
赶上例外例外
的MessageBox.show(的 ex.innerexception.Message 强>)
结束尝试
希望这可以帮助。我们在编程领域分享我们的想法真是太棒了。谢谢你的阅读。
如果您需要更多帮助。请通过flashrescueagency@gmail.com与我联系。我不是专业人士,但我在Visual Studion(VB.NET),SQL客户端和MySQL客户端编程方面有很多经验。