简体   繁体   中英

C# MySQL Syntax error in SQL Statement

I have this simple method that is supposed to insert a row into a DB. It is throwing an exception.

private void AddToLiveQueue(int user_id, bool release = false)
    {
        string sql = "INSERT INTO live_support_queues (user_id, release, created_at, updated_at)";
        sql += " VALUES(?user_id, ?release, ?created_at, ?created_at)";

        MySqlConnection conn = new MySqlConnection(connString);
        MySqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = sql;

        cmd.Parameters.AddWithValue("?user_id", user_id);
        cmd.Parameters.AddWithValue("?release", release);
        cmd.Parameters.AddWithValue("?created_at", DateTime.UtcNow);


        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        catch (Exception ex)
        {
            SendEmail email = new SendEmail();
            email.Send(ex.Message + "      " + ex.ToString());
        }
    }

I am getting this error:

"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 'release, created_at, updated_at) VALUES(70, 0, '2017-09-22 23:00:16.686741', '20' at line 1"

这是SQL列

Any help is greatly appreciated. Thanks!

release is a reserved word, and needs escaped with ` symbols if used as an identifier.

https://dev.mysql.com/doc/refman/5.7/en/keywords.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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