SQL 语句中的 C# MySQL 语法错误

标签 c# mysql

我有一个简单的方法,可以将一行插入到数据库中。它正在抛出异常。

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());
        }
    }

我收到这个错误:

"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"

Here is the SQL columns

非常感谢任何帮助。谢谢!

最佳答案

release 是一个保留字,如果用作标识符需要用`符号转义。

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

关于SQL 语句中的 C# MySQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46374326/

相关文章:

c# - 使用以点开头的正则表达式匹配字符串,不包括 double

c# - Crystal Reports如何给子报表发送参数

c# - C# 中的开源 C 编译器?

c# - 在一个类中定义 MySqlConnection 并在另一个类中使用它来处理数据库

c# - 如何将gridview中选中的行保存到sql数据库

c# - 实现 Swish 支付 C# - 仅在 Azure 上 - 发送客户端证书 - 请求被中止 : Could not create SSL/TLS secure channel

MySQL 选择数据时出错

MYSQL:如果外键不存在,如何在计数外键上返回0

php - 无法在 php 中关闭 session 和注销

php - 使用 PHP Form Builder 类提交表单后如何保留 url 变量