0

嘿,伙计们,我很难解决这个错误,请帮助我将 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;
    }
4

1 回答 1

0

string insert = "INSERT INTO 员工价值(...)";

字符串选择=“选择last_insert_id()”;

于 2012-05-07T16:05:58.570 回答