0

在最近编译并尝试测试一个基本的论坛程序集之后,我遇到了一个令人惊讶的 MySQL 错误:

Forum.ForumException: MySql.Data.MySqlClient.MySqlException (0x80004005): 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 'desc) VALUES('bar', 'bar')' at line 1 at ...

这是论坛程序集中的 CreateBoard 函数:

        public int CreateBoard(string bname, string bdesc)
        {
            MySqlConnection con = new MySqlConnection(conString);
            MySqlCommand cmd = new MySqlCommand("INSERT INTO boards (name, desc) VALUES(?name, ?desc)", con);
            int res_id = 0;

            try
            {
                //cmd.CommandText = "INSERT INTO boards(name, desc) VALUES(?name, ?desc)";
                cmd.Parameters.Add("?name", MySqlDbType.VarChar).Value = bname;
                cmd.Parameters.Add("?desc", MySqlDbType.VarChar).Value = bdesc;
                con.Open();
                cmd.ExecuteNonQuery();
                //get the id of the new board
                //cmd.CommandText = "SELECT last_insert_id() FROM boards";
                //res_id = (int)cmd.ExecuteScalar();
                return res_id;
            }
            catch (Exception yf)
            {
                throw new ForumException(yf.ToString());
            }
            finally
            {
                con.Close();
            }
        }

最后,这是来自测试页面“a.aspx”的代码片段

protected void Page_Load(object sender, EventArgs e)
{
    Forum f = new Forum();
    try
    {
        int b = f.CreateBoard("foo", "bar");
        if (b == 0)
        {
            Response.Write("Board creation failure :(");
            return;
        }
        Response.Write("Board create with id '" + b + "', name 'foo', and description 'bar'");
    }
    catch (ForumException ff)
    {
        Response.Write(ff.ToString());
    }
}

执行查询时,随机引用似乎无处不在。谁能帮我解决这个问题?我一直很沮丧解决。

4

1 回答 1

2

我认为 desc 是保留关键字,按子句降序排列。

改为使用`desc`

于 2013-01-26T06:18:52.783 回答