我是 SQL 和 C# 新手,遇到了此 SQL 错误。
The parameterized query '(@pid nvarchar(4000),@desc nvarchar(4000),@cat nvarchar(4000),@p' expects the parameter '@pid', which was not supplied.
我真的需要帮助。谢谢!
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
}
最佳答案
您必须检查每个参数是否为空。如果是,您必须传递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
关于c# - 参数化查询需要 C# SQL 未提供的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21485498/