簡體   English   中英

命令執行期間發生致命錯誤(MySQL,VBNET)

[英]Fatal error during command execution (MySQL, VBNET)

我有一個具有多個字段的表單。 並且此表單可以添加,編輯,保存和更新記錄。 但是更新按鈕給我的錯誤是“命令執行期間發生致命錯誤”,這是我不知道如何在代碼中查找錯誤。 這是我的代碼。

 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子句之間,缺少空格。

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

這是一個簡單的錯字,但我也建議避免使用AddWithValue。

AddWithValue通過查看傳入的值的數據類型來確定傳入的參數的數據類型。由於所有參數都是使用字符串創建的,因此表的所有字段都應該能夠接收字符串作為值,或者代碼和數據庫之間應該有人能夠從字符串轉換為預期的數據類型。

在本文中“我們可以停止使用AddWithValue()嗎?” 解釋了為什么應避免使用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

這樣,您可以准確指定參數的數據類型,並將正確的值傳遞給datatable字段

此錯誤主要是由於缺少或拼寫錯誤的參數聲明引起的,例如。 @FirstName誤拼寫為@FirtName

確保在SQL查詢中聲明的所有參數都在AddwithValue參數聲明中聲明。 (它有助於對查詢與Addwithvalues進行計數)。

最好的解決方案是讓Visual Studio提供有關缺失參數的信息。 使用Try-Catch塊。 在catch塊中,使用Messagebox.show(ex.Innerexception.Message)而不是Messagebox.show(ex.message) 這將顯示缺少的確切參數,例如。 下面。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM