如何解决未处理的异常错误?

时间:2019-05-25 00:01:08

标签: mysql vb.net

单击表单中的更新按钮时出现此错误:

  

“ System.Data.dll中发生了类型为'System.Data.OleDb.OleDbException'的未处理的异常

     

其他信息:“ intGenderID”附近的语法不正确。”

此更新不起作用。 有人能指出我正确的方向吗?预先感谢!

Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click

        Dim strSelect As String = ""
        Dim strFirstName As String = ""
        Dim strLastName As String = ""
        Dim strAddress As String = ""
        Dim strCity As String = ""
        Dim strState As String = ""
        Dim strZip As String = ""
        Dim strPhoneNumber As String = ""
        Dim strEmail As String = ""
        Dim intRowsAffected As Integer


        Dim cmdUpdate As OleDb.OleDbCommand


        If Validation() = True Then
            ' open database
            If OpenDatabaseConnectionSQLServer() = False Then

                ' No, warn the user ...
                MessageBox.Show(Me, "Database connection error." & vbNewLine &
                                    "The application will now close.",
                                    Me.Text + " Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error)


                Me.Close()

            End If


            If Validation() = True Then

                strFirstName = txtFirstName.Text
                strLastName = txtLastName.Text
                strAddress = txtAddress.Text
                strCity = txtCity.Text
                strState = txtState.Text
                strZip = txtZip.Text
                strPhoneNumber = txtPhoneNumber.Text
                strEmail = txtEmail.Text




                strSelect = "Update TGolfers Set strFirstName = '" & strFirstName & "', " & "strLastName = '" & strLastName &
                "', " & "strAddress = '" & strAddress & "', " & "strCity = '" & strCity & "', " &
                 "strState = '" & strState & "', " & "strZip = '" & strZip & "', " &
                 "strPhoneNumber = '" & strPhoneNumber & "', " & "strEmail = '" & strEmail & "', " &
                 "intShirtSizeID = '" & cboShirtSizes.SelectedValue.ToString & "' " &
                 "intGenderID = '" & cboGenders.SelectedValue.ToString & "' " &
                 "Where intGolferID = " & cboGolfers.SelectedValue.ToString



                MessageBox.Show(strSelect)



                cmdUpdate = New OleDb.OleDbCommand(strSelect, m_conAdministrator)


                intRowsAffected = cmdUpdate.ExecuteNonQuery()


                If intRowsAffected = 1 Then
                    MessageBox.Show("Update successful")
                Else
                    MessageBox.Show("Update failed")
                End If


                CloseDatabaseConnection()


                frmManageGolfers_Load(sender, e)
            End If
        End If

    End Sub

2 个答案:

答案 0 :(得分:0)

语法错误表示SQL语法不正确。非常严格。

在'intGenderID'附近意味着语法错误就在此之前。就您而言,您错过了一个逗号。

答案 1 :(得分:0)

我将继续进行此MySql。将数据库对象放在本地。您需要跟踪它们是否已关闭并丢弃。 `使用...结尾使用块会照顾到这一点,即使出现错误也是如此。

始终使用参数。不仅使编写sql语句更加容易,而且还可以从sql注入中节省数据库。

在线附加评论。

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim intRowsAffected As Integer
    Dim strSelect As String = "Update TGolfers Set strFirstName = @FirstName, strLastName = @LastName, strAddress = @Address, strCity = @City, strState = @State, strZip = @Zip, strPhoneNumber = @Phone, strEmail = @EMail, intShirtSizeID = @ShirtSize, intGenderID = @Gender Where intGolferID = @GolferID;"
    If Not Validation() Then
        'Actually the input should be validated before we get here
        MessageBox.Show("Did not pass validation. Correct the input")
        Return
    End If
    Using cn As New MySqlConnection("Your connection string")
        Using cmd As New MySqlCommand(strSelect, cn)
            cmd.Parameters.Add("@FirstName", MySqlDbType.VarChar).Value = txtFirstName.Text
            cmd.Parameters.Add("@LastName", MySqlDbType.VarChar).Value = txtLastName.Text
            cmd.Parameters.Add("@Address", MySqlDbType.VarChar).Value = txtAddress.Text
            cmd.Parameters.Add("@City", MySqlDbType.VarChar).Value = txtCity.Text
            cmd.Parameters.Add("@State", MySqlDbType.VarChar).Value = txtState.Text
            cmd.Parameters.Add("@Zip", MySqlDbType.VarChar).Value = txtZip.Text
            cmd.Parameters.Add("@Phone", MySqlDbType.VarChar).Value = txtPhoneNumber.Text
            cmd.Parameters.Add("@EMail", MySqlDbType.VarChar).Value = txtEmail.Text
            'Are you sure you have set the .ValueMember of the combo boxes?
            cmd.Parameters.Add("@ShirtSize", MySqlDbType.VarChar).Value = cboShirtSizes.SelectedValue.ToString
            cmd.Parameters.Add("@Gender", MySqlDbType.VarChar).Value = cboGenders.SelectedValue.ToString
            'Are your sure that intGolferID is not a number
            cmd.Parameters.Add("@GolferID", MySqlDbType.Int32).Value = cboGolfers.SelectedValue
            cn.Open()
            intRowsAffected = cmd.ExecuteNonQuery()
        End Using
    End Using
    If intRowsAffected = 1 Then
        MessageBox.Show("Update successful")
    Else
        MessageBox.Show("Update failed")
    End If
    frmManageGolfers.Show() 'I can't image why you would try to send a button and the button's event args to the Load event of another form
End Sub