我使用了参数化查询将数据保存到SQL Server。执行时抛出以下错误:
参数化查询'(@CustomerID int,@SerialNo nvarchar(2),@ModelNo nvarchar(2),@Sal'期望提供名为“@CreatedBy”的参数,但没有提供。
以下是我的代码:
public static bool SaveSales(int custid, string model, string serial, DateTime salesdate, decimal price, string mainservice, string comments, DateTime createddate, string createdby, DateTime modifieddate, string modifiedby)
{
bool saved = false;
try
{
using (SqlConnection conn = new SqlConnection(DBManager.DBConnection))
{
conn.Open();
using (var command = conn.CreateCommand())
{
command.CommandText = "INSERT INTO tbl_Sales VALUES(@CustomerID, @SerialNo, @ModelNo, @SalesDate, @Price, @MaintanenceService, @Comments, @CreatedDate, @CreatedBy, @ModifiedDate, @ModifiedBy)";
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@CustomerID", custid);
command.Parameters.AddWithValue("@SerialNo", serial);
command.Parameters.AddWithValue("@ModelNo", model);
command.Parameters.AddWithValue("@SalesDate", salesdate);
command.Parameters.AddWithValue("@Price", price);
command.Parameters.AddWithValue("@MaintanenceService", mainservice);
command.Parameters.AddWithValue("@Comments", comments);
command.Parameters.AddWithValue("@CreatedDate", createddate);
command.Parameters.AddWithValue("@CreatedBy", createdby);
command.Parameters.AddWithValue("@ModifiedDate", modifieddate);
command.Parameters.AddWithValue("@ModifiedBy", modifiedby);
command.ExecuteNonQuery();
}
conn.Close();
}
saved = true;
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return saved;
}
return saved;
}
我的查询有什么问题?我使用参数化查询来正确保存销售日期。非常感谢您的帮助。
INSERT INTO tbl_Sales (Column1, Column2, ...) VALUES (@Param1, @Param2, ...)
)。 - Tetsuya YamamotoAddWithValue
方法。该方法必须从传递的值中“猜测”实际类型和大小。如果该值为空,那么无法猜测正确的类型。 - Panagiotis KanavosINSERT
;特别是当一个是全新的,而另一个是旧有的时,很容易在不同的安装/平台上出现列的不同顺序。 - Marc Gravell