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

发布于 2024-12-02 01:44:52 字数 1564 浏览 1 评论 0原文

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

 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

异常消息是:

<块引用>

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

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

I was having problems updating information in my SQL database using my vb.net application, but recently I found the solution. However now I have run into another problem which is shown in the code below:

 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

The exception message is:

Fatal error encountered during command execution.

I don't know if this is simply a syntax error that can be fixed easily, or something to do with my database configuration.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾挽 2024-12-09 01:44:52

如果我正确地解读了您的屏幕截图,那么您的 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 = ....... "

If I decipher your screen shot correctly, then your UPDATE statement is being put together:

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

This results in:

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

so you're really only missing some spaces!

"UPDATE event SET " &    -- observe the SPACE after the SET !
"status = ?Status " &    -- observe the SPACE after the ?Status
"WHERE user_id = ....... "
晨曦÷微暖 2024-12-09 01:44:52

之前添加空格;在下面

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

谢谢

Add space before ; on below

conn.ConnectionString = "server=" & frmLogin.txtserver.Text & ";" _

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文