简体   繁体   English

mysql vb.net 中用于更新查询的正确语法是什么?

[英]What's the correct syntax in mysql vb.net for update query?

this is my sample code这是我的示例代码

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        myConn = New MySqlConnection
    myConn.ConnectionString = "server=localhost;userid=root;password=root;database=itss"
    Dim reader As MySqlDataReader

    Try
        myConn.Open()
        Dim query As String
        query = "update itss.announcement set annoMessage = '" & TextBox1.Text & "' where idAnno = 3"
        command = New MySqlCommand(query, myConn)
        reader = command.ExecuteReader
        myConn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        myConn.Dispose()
    End Try
    'strMessage = TextBox1.Text
    Form1.GroupBox1.Select()
    Form1.Activate()
    Form4.Activate()
    Me.Hide()
End Sub

when I clicked the button an exception appears saying当我点击按钮时,出现一个异常说

You have an error in your sql syntax;你的 sql 语法有错误; check the manual that corresponds to your mysql server version for the right syntax to use near 's breakthrough to challenge yesterday's old pattern, and to create tomorrow's n' at line 1检查与您的 mysql 服务器版本相对应的手册以获取正确的语法,以使用 Near 的突破来挑战昨天的旧模式,并在第 1 行创建明天的 n'

The problem is that you're using reader = command.ExecuteReader on UPDATE statement with concatenated argument string.问题是您在带有连接参数字符串的UPDATE语句上使用reader = command.ExecuteReader You should use ExecuteNonQuery with parameterized query like this:您应该将ExecuteNonQuery与参数化查询一起使用,如下所示:

myConn.Open()
Dim query As String
query = "update itss.announcement set annoMessage = @message where idAnno = 3"
command = New MySqlCommand(query, myConn)
command.Parameters.Add("@message", MySqlDbType.VarChar).Value = TextBox1.Text
reader = command.ExecuteNonQuery() ' this is used for UPDATE statement
myConn.Close()

NB: ExecuteReader used to return query results from SELECT statement, ie SELECT * FROM itss.announcement WHERE idAnno = 3 .注意: ExecuteReader用于从SELECT语句返回查询结果,即SELECT * FROM itss.announcement WHERE idAnno = 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM