How can I resolve a fatal SQL statement error?
I was having problems updating information in 开发者_如何学JAVAmy 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.
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 = ....... "
Add space before ; on below
conn.ConnectionString = "server=" & frmLogin.txtserver.Text & ";" _
Thanks
精彩评论