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

标签 c# sql

我是 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/

相关文章:

c# - Xmlns 到项目文件夹中的类?

c# - 带资源文件的 asp.net 核心 (resx)

php - 单个自动编号而不是多个键

mysql - 如何获取每个用户的评分?

c# - 不在 asp.net core 中的 HttpRequestMessage 和 HttpResponseMessage 上调用 Dispose

c# - 如何在 asp.net 和数据库是 mysql 中使用 datetime 数据类型传递空值?

C# XmlSerializer 强制编码类型为 ISO-8859-1

mysql - MySQL 中 MINUS 子句的替代方案?

java - Hibernate HQL NULL 比较模式

sql - SQL Server 如何处理以下查询?