mysql查询scope_identity错误

时间:2012-05-07 15:43:20

标签: c# mysql

嘿,伙计们我正在努力解决这个错误,请帮忙 我使用c#与mysql连接器,我写了一个类似这样的查询......

INSERT INTO employee VALUES(...); SELECT last_insert_id();

但我因某种原因不断收到此错误:(

"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 'SELECT scope_identity()' at line 1".

我没有得到关于这个错误的部分是我没有在我的代码中的任何地方写scope_identity()

我甚至删除了查询的SELECT last_insert_id();部分,但由于一些奇怪的原因,我仍然会遇到相同的错误。

[编辑] 这会完美地生成查询,但最后会添加scope_identity()而不是last_insert_id()

public static MySqlCommand insert(string tableName, params object[] values)
    {
        string sqlQuery = "INSERT INTO " + tableName + " VALUES(";

        for (int i = 0; i < values.Count(); i++)
        {
            if (i == values.Count() - 1 && values[i] is string)
            {
                sqlQuery = sqlQuery + "'" + values[i] + "'";
            }
            else if (i == values.Count() - 1 && values[i] is int)
            {
                sqlQuery = sqlQuery + values[i];
            }
            else if (values[i] is string || values[i] is DateTime)
            {
                sqlQuery = sqlQuery + "'" + values[i] + "', ";
            }
            else if (values[i] is int)
            {
                sqlQuery = sqlQuery + values[i] + ", ";
            }
        }

        sqlQuery = sqlQuery + "); SELECT LAST_INSERT_ID(); ";

        MySqlCommand cmd = new MySqlCommand(sqlQuery);

        return cmd;
    }

1 个答案:

答案 0 :(得分:0)

string insert =“INSERT INTO employee VALUES(...)”;

string select =“SELECT last_insert_id()”;