【问题标题】:The parameterized query expects the parameter which was not supplied c# SQL参数化查询需要未提供的参数 c# SQL
【发布时间】:2014-01-31 16:48:50
【问题描述】:

我是 SQL 和 C# 的新手,我遇到了这个 SQL 错误。

参数化查询 '(@pid nvarchar(4000),@desc nvarchar(4000),@cat nvarchar(4000),@p' 需要参数'@pid', 没有提供。

我真的需要帮助。谢谢!

 public void InsertRecord()
    {
        SqlCommand cmd = new SqlCommand("INSERT INTO PRODUCTS VALUES (@pid, @desc, @cat, @price, @scode)", myCon);
        cmd.Parameters.AddWithValue("@pid", productID);
        cmd.Parameters.AddWithValue("@desc", description);
        cmd.Parameters.AddWithValue("@cat", category);
        cmd.Parameters.AddWithValue("@price", price);
        cmd.Parameters.AddWithValue("@scode", supplierCode);//corrected the "key codes"

        myCon.Open();
        cmd.ExecuteNonQuery();
        myCon.Close();//added these lines of codes
    }

【问题讨论】:

  • productID 是否为空?
  • 您使用的 INSERT 语法要求传递所有字段值以进行插入,您的表 PRODUCTS 中是否只有 5 个字段?
  • 我无法启用 productID 文本框。我不知道为什么。
  • 我的意思是文本框已启用,但我无法在其中输入任何内容。
  • C# 中的 Windows 窗体应用程序

标签: c# sql


【解决方案1】:

您必须检查每个参数是否为空。如果是,你必须通过DBNull.Value

SqlParameter pidParam = command.Parameters.AddWithValue("@pidParam", productID);
if (productID == null)
{
    pidParam.Value = DBNull.Value;
}

来源:The parameterized query ..... expects the parameter '@units', which was not supplied

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 2016-04-16
    相关资源
    最近更新 更多