【发布时间】:2014-12-12 20:01:28
【问题描述】:
我正在使用带有 C# 的 MySQL,但我遇到了问题。为什么这段代码不起作用?
MySqlCommand cmd = new MySqlCommand("myConnectionString");
MySqlParameter lastId = new MySqlParameter();
lastId.ParameterName = "@LastID";
lastId.Value = 0;
lastId.Direction = System.Data.ParameterDirection.Output;
this.Command.Parameters.Add(lastId);
this.Command.CommandText = "SET @LastID = 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 '0 = LAST_INSERT_ID()'
this.Command.ExecuteNonQuery();
【问题讨论】:
-
你告诉我们为什么?什么不起作用?你得到什么错误?您尝试过什么来解决此问题?
-
一方面,@LastID 是您传入的参数,但您试图将其设置为变量。就像错误所说的那样,您正在尝试将 LAST_INSERT_ID() 的值分配给另一个值 0。
-
Select LAST_INSERT_ID()和var lastId = Command.ExecuteScalar();怎么样。不需要out参数?