在使用 C# 的参数化查询时抛出异常

4

我使用了参数化查询将数据保存到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 查询中没有提到列名(例如 INSERT INTO tbl_Sales (Column1, Column2, ...) VALUES (@Param1, @Param2, ...))。 - Tetsuya Yamamoto
1
请确保createdBy不为空。更改您的代码如下:createdBy == null ? "" : createdBy - Taimur Khan
你可以检查一下这个是否解决了你的问题吗?https://dev59.com/L2865IYBdhLWcg3wU9GI - Lasse V. Karlsen
不要使用AddWithValue方法。该方法必须从传递的值中“猜测”实际类型和大小。如果该值为空,那么无法猜测正确的类型。 - Panagiotis Kanavos
顺便提一下:通常不建议在没有明确引用插入列顺序的情况下使用INSERT;特别是当一个是全新的,而另一个是旧有的时,很容易在不同的安装/平台上出现列的不同顺序。 - Marc Gravell
显示剩余2条评论
1个回答

4

我怀疑createdbynull,这意味着它不会被发送 - 它需要是DBNull

尝试:

command.Parameters.AddWithValue("@CreatedBy", createdby ?? (object)DBNull.Value);

或者:尝试使用像Dapper这样的工具,它会a:使整个过程更加容易,b:做得正确;例如:
using (SqlConnection conn = new SqlConnection(DBManager.DBConnection))
{
    conn.Execute("INSERT INTO tbl_Sales VALUES(@CustomerID, @SerialNo, @ModelNo, @SalesDate, @Price, @MaintanenceService, @Comments, @CreatedDate, @CreatedBy, @ModifiedDate, @ModifiedBy)",
      new {
        CustomerID = custid, SerialNo = serial,
        ModelNo = model, SalesDate = salesdate,
        Price = price, MaintanenceService = mainservice,
        Comments = comments, CreatedDate = createddate,
        CreatedBy = createdby, ModifiedDate = modifieddate,
        ModifiedBy = modifiedby });
}

甚至更简单:
using (SqlConnection conn = new SqlConnection(DBManager.DBConnection))
{
    conn.Execute("INSERT INTO tbl_Sales VALUES(@custid, @serial, @model, @salesdate, @price, @mainservice, @comments, @createddate, @createdby, @modifieddate, @modifiedby )",
      new {
        custid, serial, model, salesdate,
        price, mainservice, comments, createddate,
        createdby, modifieddate, modifiedby });
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接