问题描述
问题属于这个按钮,我在这里得到错误:
the problem falls under this button and i get the error here:
objcommand.ExecuteNonQuery()
一旦我按下这个按钮,它就会更新我的 mysql 数据库中的所有记录.这是我的代码(确切的代码适用于另一个表):
Once i press this button, it should update all the records in my mysql database. Here is my code (the exact code works fine for another table):
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
推荐答案
你写的
objcommand.Parameters.AddWithValue("@postion", position)
但这应该是
objcommand.Parameters.AddWithValue("@position", position)
其他问题可能是,
您的 SQL 语句中有 ni=@ninumber
,
但是您在宿主变量中使用 objcommand.Parameters.AddWithValue("@ni", ninumber)
并且在您的 SQL 语句中,这看起来与您对其他主机变量的使用不同companyname= @company
and in your SQL statement this looks different companyname= @company
compared to your usage of other host variables
您是否可以尝试使用与您在 SQL 语句中使用的 objcommand.Parameters.AddWithValue("@host_variable", variable)
相同的顺序
and could you try to use the same order of objcommand.Parameters.AddWithValue("@host_variable", variable)
as you use in your SQL statement
这篇关于MySql 异常未处理 - 命令执行期间遇到致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!