参数化查询需要未提供的参数 c# SQL

Yel*_*ine 2 c# sql

我是 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
    }
Run Code Online (Sandbox Code Playgroud)

Bar*_*ann 5

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

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

来源:参数化查询 ..... 需要参数“@units”,但未提供