我在将数据输入SQL Server 2008数据库时遇到了一个奇怪的问题,错误告诉我
System.Data.SqlClient.SqlException未处理HResult = -2146232060 消息=已声明变量名称'@comments'。 变量名在查询批处理中必须是唯一的或存储的 程序。必须声明标量变量“@employeeId”。
问题是......我确实宣布了他们两个,我错过了什么吗?
public bool Addfix(DateTime receiveDate, int clientId, int modelID, string problem, string comment, int employeeId, float priceOffer, float price)
{
_cmd.CommandText ="INSERT INTO fixes(receiveDate, clientId, modelID, problem, comments, employeeId, priceOffer, price, returned) VALUES (@receiveDate, @clientId, @modelID, @problem, @comments, @employeeId, @priceOffer,@price,@returned);";
_cmd.Parameters.Add("@receiveDate", SqlDbType.Date).Value = receiveDate;
_cmd.Parameters.AddWithValue("@clientId", clientId);
_cmd.Parameters.AddWithValue("@modelID", modelID);
_cmd.Parameters.AddWithValue("@problem", problem);
_cmd.Parameters.AddWithValue("@comments", comment);
_cmd.Parameters.AddWithValue("@employeeId", employeeId);
_cmd.Parameters.AddWithValue("@priceOffer", priceOffer);
_cmd.Parameters.AddWithValue("@price", price);
_cmd.Parameters.AddWithValue("@returned ", 0);
_con.Open();
_cmd.ExecuteNonQuery();
_con.Close();
return true;
}
答案 0 :(得分:1)
查看函数定义,您的sqlcommand / sqlconnectin对象似乎是类级别或全局的。在您的情况下,您似乎使用了具有@comments参数的_cmd对象,并且在执行此方法之前未被清除。 始终不鼓励使用全局sqlcommand和sqlconnection对象,但由于某些不可避免的原因,您需要使用全局sqlcommand,因此最好清除命令对象的参数,例如: sqlcommandObject.Parameters.Clear()。