MySQL 查询scope_identity错误
本文关键字:identity 错误 scope 查询 MySQL | 更新日期: 2023-09-27 18:33:29
嘿伙计们,我很难解决此错误,请帮助我使用带有 MySQL 连接器的 C#,我写了一个查询,如下所示......
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;
}
字符串插入 = "插入员工值(...)";
字符串选择="选择 last_insert_id()";