[英]mysql query scope_identity error
大家好,我很難解決這個錯誤,請幫助我在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;
}
字符串插入=“插入雇員的值(...)”;
字符串選擇=“ SELECT last_insert_id()”;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.