gpt4 book ai didi

c# - 参数化查询 ..... 需要参数 '@units' ,但未提供

转载 作者:IT王子 更新时间:2023-10-29 03:46:52 31 4
gpt4 key购买 nike

我遇到了这个异常:

The parameterized query '(@Name nvarchar(8),@type nvarchar(8),@units nvarchar(4000),@rang' expects the parameter '@units', which was not supplied.

我的插入代码是:

public int insertType(string name, string type, string units = "N\\A", string range = "N\\A", string scale = "N\\A", string description = "N\\A", Guid guid = new Guid())
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand();
command.CommandText = "INSERT INTO Type(name, type, units, range, scale, description, guid) OUTPUT INSERTED.ID VALUES (@Name, @type, @units, @range, @scale, @description, @guid) ";
command.Connection = connection;
command.Parameters.AddWithValue("@Name", name);
command.Parameters.AddWithValue("@type", type);
command.Parameters.AddWithValue("@units", units);
command.Parameters.AddWithValue("@range", range);
command.Parameters.AddWithValue("@scale", scale);
command.Parameters.AddWithValue("@description", description);
command.Parameters.AddWithValue("@guid", guid);
return (int)command.ExecuteScalar();
}
}

异常是一个惊喜,因为我正在使用 AddWithValue 函数并确保我为该函数添加了一个默认参数。

已解决:

问题是某些参数为空字符串(覆盖默认值)

这是工作代码:

public int insertType(string name, string type, string units = "N\\A", string range = "N\\A", string scale = "N\\A", string description = "N\\A", Guid guid = new Guid())
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand();
command.CommandText = "INSERT INTO Type(name, type, units, range, scale, description, guid) OUTPUT INSERTED.ID VALUES (@Name, @type, @units, @range, @scale, @description, @guid) ";
command.Connection = connection;
command.Parameters.AddWithValue("@Name", name);
command.Parameters.AddWithValue("@type", type);

if (String.IsNullOrEmpty(units))
{
command.Parameters.AddWithValue("@units", DBNull.Value);
}
else
command.Parameters.AddWithValue("@units", units);
if (String.IsNullOrEmpty(range))
{
command.Parameters.AddWithValue("@range", DBNull.Value);
}
else
command.Parameters.AddWithValue("@range", range);
if (String.IsNullOrEmpty(scale))
{
command.Parameters.AddWithValue("@scale", DBNull.Value);
}
else
command.Parameters.AddWithValue("@scale", scale);
if (String.IsNullOrEmpty(description))
{
command.Parameters.AddWithValue("@description", DBNull.Value);
}
else
command.Parameters.AddWithValue("@description", description);




command.Parameters.AddWithValue("@guid", guid);


return (int)command.ExecuteScalar();
}


}

最佳答案

试试这段代码:

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

并且您必须检查所有其他参数是否为空值。如果它为 null,则必须传递 DBNull.Value 值。

关于c# - 参数化查询 ..... 需要参数 '@units' ,但未提供,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23448403/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com