问题属于此按钮,我在这里收到错误:
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
最佳答案
你写
objcommand.Parameters.AddWithValue("@postion", position)
但这可能应该是
objcommand.Parameters.AddWithValue("@position", position)
其他问题可能是,
你有ni=@ninumber
,在您的 SQL 语句中,
但你使用 objcommand.Parameters.AddWithValue("@ni", ninumber)
在你的主机变量中
在你的 SQL 语句中这看起来不同 companyname= @company
与您使用其他主机变量相比
您可以尝试使用与 objcommand.Parameters.AddWithValue("@host_variable", variable)
相同的顺序吗?正如您在 SQL 语句中使用的那样
关于MySql 异常未处理 - 命令执行期间遇到 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22501881/