error in mysql syntax in vb.net

若如初见. 提交于 2019-12-12 01:18:28

问题


I get this error, while I'm testing the code below:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[student](ID, LASTNAME, FIRSTNAME, SCHOOL) VALUES ('333', 'aaa', 'aaa', 'aaa')' at line 1

I just recycled the code that I used in manipulating ms sql database. So the syntax must be wrong. What might be the correct syntax for adding records into mysql database? Here is my current code:

 idnum = TextBox1.Text
        lname = TextBox2.Text
        fname = TextBox3.Text
        skul = TextBox4.Text


        Using sqlcon As New MySqlConnection("Server=localhost; Database=testing;Uid=root;Pwd=nitoryolai123$%^;")

            sqlcon.Open()
            Dim sqlcom As New MySqlCommand()
            sqlcom.Connection = sqlcon

            sqlcom.CommandText = "INSERT INTO [student](ID, LASTNAME, FIRSTNAME, SCHOOL) VALUES (@ParameterID, @ParameterLastName, @ParameterFirstName, @ParameterSchool)"

            sqlcom.Parameters.AddWithValue("@ParameterID", TextBox1.Text)
            sqlcom.Parameters.AddWithValue("@ParameterLastName", TextBox2.Text)
            sqlcom.Parameters.AddWithValue("@ParameterFirstName", TextBox3.Text)
            sqlcom.Parameters.AddWithValue("@ParameterSchool", TextBox4.Text)

            sqlcom.ExecuteNonQuery()

        End Using

Please help, thanks


回答1:


Try removing the square brackets from the student table name, and adding a space between table name and column list: [student] (ID, ...

Without your table schema definition I can't be 100% sure, but ParameterID appears to be an int, but you are passing as a string (i.e. value is wrapped in single quotes). Try converting TextBox1.Text to an int first (using Try.Parse)



来源:https://stackoverflow.com/questions/2440924/error-in-mysql-syntax-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!