如何解决致命的SQL语句错误?

时间:2011-08-29 12:19:53

标签: mysql database vb.net syntax

我在使用我的vb.net应用程序更新SQL数据库中的信息时遇到了问题,但最近I found the solution。但是现在我遇到了另一个问题,如下面的代码所示:

 Private Sub cmdupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdupdate.Click
   Dim conn As New MySqlConnection
    Dim myCommand As New MySqlCommand

    '#######
    conn.ConnectionString = "server=" & frmLogin.txtserver.Text & ";" _
& "user id=" & frmLogin.txtusername.Text & ";" _
& "password=" & frmLogin.txtpassword.Text & ";" _
& "database=in_out"
    '#######

    myCommand.Connection = conn
    myCommand.CommandText = "UPDATE event SET" _
        & "status = ?Status " _
        & "WHERE user_id = ?UserID AND message_id = ?MessageID AND creator = ?Creator"

    myCommand.Parameters.AddWithValue("?UserID", myUserID)
    myCommand.Parameters.AddWithValue("?MessageID", cbomessage.SelectedValue)
    myCommand.Parameters.AddWithValue("?Status", cbostatus.SelectedItem)
    myCommand.Parameters.AddWithValue("?Creator", myUserID)
    myCommand.Connection = conn
    Try
        myCommand.Connection.Open()
        myCommand.ExecuteNonQuery()
        myCommand.Connection.Close()
    Catch myerror As MySqlException
        MsgBox("There was an error updating the database: " & myerror.Message)
    End Try
End Sub

异常消息是:

  
    

命令执行期间遇到致命错误。

  

我不知道这是否只是一个可以轻松解决的语法错误,或者与我的数据库配置有关。

2 个答案:

答案 0 :(得分:1)

如果我正确解读了您的屏幕截图,那么您的UPDATE语句将被放在一起:

"UPDATE event SET" &
"status = ?Status" &
"WHERE user_id = ....... "

这导致:

UPDATE event SETstatus = ?StatusWHERE user_id = .......

所以你真的只缺少一些空格

"UPDATE event SET " &    -- observe the SPACE after the SET !
"status = ?Status " &    -- observe the SPACE after the ?Status
"WHERE user_id = ....... "

答案 1 :(得分:0)

之前添加空格;在下面

conn.ConnectionString =“server =”& frmLogin.txtserver.Text& “;” _

由于