命令执行期间发生致命错误(MySQL,VBNET)

Fatal error during command execution (MySQL, VBNET)

我有一个包含多个字段的表单。并且这个表单可以添加、编辑、保存和更新记录。但是更新按钮给我错误 "Fatal error during command execution" 这是我不知道如何在我的代码中找到错误。这是我的代码。

 Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As    System.EventArgs) Handles btnUpdate.Click
    conn = New MySqlConnection
    conn.ConnectionString = "server=localhost; userid=root; password=root;   database=dbase"

    Try

        conn.Open()
        Sql = "UPDATE dbase.tblfacultyinfo SET firstname = @firstname, " & _
                                        "middlename = @middlename," & _
                                        "lastname = @lastname," & _
                                        "gender = @gender," & _
                                        "birthdate = @birthdate," & _
                                        "age = @age," & _
                                        "emailAdd = @emailAdd," & _
                                        "contact_mobileno = @contact_mobileno," & _
                                        "contact_telno = @contact_telno," & _
                                        "homeadd_houseno = @homeadd_houseno," & _
                                        "homeadd_street = @homeadd_street," & _
                                        "homeadd_brgy = @homeadd_brgy," & _
                                        "homeadd_town = @homeadd_town," & _
                                        "tersiary_schoolname = @tersiary_schoolname," & _
                                        "tersiary_address = @tersiary_address," & _
                                        "tersiary_degree = @tersiary_degree," & _
                                        "tersiary_batch = @tersiary_batch," & _
                                        "secondary_schoolname = @secondary_schoolname," & _
                                        "secondary_address = @secondary_address," & _
                                        "secondary_batch = @secondary_batch" & _
                                        "WHERE facultyNo = @facultyNo"
        With cmd
            .Connection = conn
            .CommandText = Sql
            .CommandType = CommandType.Text
            .Parameters.AddWithValue("@facultyNo", txtFacultyNo.Text)
            .Parameters.AddWithValue("@firstname", txtFirstname.Text)
            .Parameters.AddWithValue("@middlename", txtMiddlename.Text)
            .Parameters.AddWithValue("@lastname", txtLastname.Text)
            .Parameters.AddWithValue("@gender", cbGender.Text)
            .Parameters.AddWithValue("@birthdate", dtpBirthdate.Value)
            .Parameters.AddWithValue("@age", txtAge.Text)
            .Parameters.AddWithValue("@emailAdd", txtEmailAdd.Text)
            .Parameters.AddWithValue("@contact_mobileno", txtContact_Mobile.Text)
            .Parameters.AddWithValue("@contact_telno", txtContact_Tel.Text)
            .Parameters.AddWithValue("@homeadd_houseno", txtHomeAdd_HouseNo.Text)
            .Parameters.AddWithValue("@homeadd_street", txtHomeAdd_Street.Text)
            .Parameters.AddWithValue("@homeadd_brgy", txtHomeAdd_Brgy.Text)
            .Parameters.AddWithValue("@homeadd_town", txtHomeAdd_Town.Text)
            .Parameters.AddWithValue("@tersiary_schoolname", txtTersiary_Schoolname.Text)
            .Parameters.AddWithValue("@tersiary_address", txtTersiary_Add.Text)
            .Parameters.AddWithValue("@tersiary_degree", txtTersiary_Degree.Text)
            .Parameters.AddWithValue("@tersiary_batch", cbBatchTer.Text)
            .Parameters.AddWithValue("@secondary_schoolname", txtSecondary_Schoolname.Text)
            .Parameters.AddWithValue("@secondary_address", txtSecondary_Add.Text)
            .Parameters.AddWithValue("@secondary_batch", cbBatchSec.Text)
        End With
        dr = cmd.ExecuteReader
        MsgBox("Data Updated", vbInformation, "Successfully Update Record")
        conn.Close()


    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        conn.Dispose()
        buttons("ResetButtons")
        DataGridView1.Enabled = True
    End Try
End Sub

感谢任何帮助。提前致谢。

您的语法有误。在最后一个参数和 WHERE 子句之间缺少 space。

 "secondary_batch = @secondary_batch" & _
 " WHERE facultyNo = @facultyNo"
  ^

这是一个简单的错字,但我也建议避免使用 AddWithValue。

AddWithValue 根据传入值的数据类型决定传递参数的数据类型。因为所有参数都是使用字符串创建的,所以 table 的所有字段都应该能够接收字符串作为值或者您的代码和数据库之间的某个人应该能够将字符串转换为预期的数据类型。

本文 'Can we stop using AddWithValue() already?' 解释了为什么应避免使用 AddWithValue(或至少要格外小心地使用)。

例如,您的年龄字段可能是一个数字数据字段。所以,我会使用

而不是 AddWithValue
Dim age as Integer
if Int32.TryParse(txtAge.Text, age) = False Then
    // Error message and exit from the save
....

cmd.Parameters.Add(New SqlParameter("@age", SqlDbType.Int)).Value = age

通过这种方式,您可以准确指定参数的数据类型,并将正确的值传递给您的数据table字段

此错误主要是由于缺少或拼写错误的参数声明引起的,例如。 @FirstName 被错误拼写为 @FirtName

确保在 SQL 查询中声明的所有参数都在 AddwithValue 参数声明中声明。 (它有助于计算查询与 Addwithvalues)。

最好的解决方案是 Visual Studio 提供有关缺少参数的信息。使用 Try-Catch 块。在 catch 块中使用 Messagebox.show(ex.Innerexception.Message) 而不是 Messagebox.show(ex.message)。这将显示缺少的 exact 参数,例如。下面。

Try
      conCommand.Parameters.Addwithvalue("@FirstName", txtFirstName.text)
      conCommand.Parameters.Addwithvalue("@MiddleName", txtMiddleName.text)
      conCommand.Parameters.Addwithvalue("@LastName", txtLastName.text)
      conCommand.Parameters.Addwithvalue("@PhoneNo", txtPhoneno.text) 
catch ex as exception          
Messagebox.show(ex.innerexception.Message)
End Try