烦人的MySQL错误(尝试了一切,没有任何工作)

时间:2013-01-26 06:16:26

标签: mysql

在最近编译并尝试测试基本论坛程序集后,我遇到了一个令人惊讶的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());
    }
}

在执行查询时,似乎无处不在。有人可以帮我这个吗?我很难解决。

1 个答案:

答案 0 :(得分:2)

我认为desc是一个保留关键字,按顺序降序。

做`desc`而不是