该参数化查询.....预计参数'@units',但未提供该 [英] The parameterized query ..... expects the parameter '@units', which was not supplied
问题描述
我得到这个异常:
的参数化查询(@Name为nvarchar(8),@类型为nvarchar(8) @单位为nvarchar(4000),@响'需要参数'@units,这是没有提供。
块引用>
我的代码插入是:
公众诠释insertType(字符串名称,字符串类型,字符串单元=N\\A,串范围=N\\A,串规模=N\\A,字符串描述=N\\A的Guid GUID =新的GUID())
{
使用(SqlConnection的连接=新的SqlConnection(的connectionString))
{
connection.Open();
的SqlCommand命令=新的SqlCommand();
command.CommandText =INSERT入式(名称,类别,单位,范围,规模,描述,GUID)输出INSERTED.ID VALUES(@Name,@type,@units,@range,@scale,@description,@guid);
command.Connection =连接;
command.Parameters.AddWithValue(@名,名);
command.Parameters.AddWithValue(@类型,输入);
command.Parameters.AddWithValue(@单位,单位);
command.Parameters.AddWithValue(@范围,范围);
command.Parameters.AddWithValue(@规模,规模);
command.Parameters.AddWithValue(@描述,描述);
command.Parameters.AddWithValue(@ GUID,GUID)
回报(INT)command.ExecuteScalar();
}
}
唯一的例外是一个惊喜,因为我使用的
AddWithValue
功能,并确保我加了一个默认参数的功能。
解决:
的问题是,一些参数,其中,空字符串(即覆盖默认)
这是工作代码:
公众诠释insertType(字符串名称,字符串类型,字符串单元=N\\A,字符串范围=N\\A,串规模=N\\A,字符串描述=N\\A中GUID GUID =使用新的GUID())
{
(SqlConnection的连接=新的SqlConnection(的connectionString))
{
connection.Open();
的SqlCommand命令=新的SqlCommand();
command.CommandText =INSERT INTO类型(名称,类别,单位,范围,规模,描述,GUID)输出INSERTED.ID VALUES(@Name,@type,@units,@range,@scale,@description ,@guid);
command.Connection =连接;
command.Parameters.AddWithValue(@名,名);
command.Parameters.AddWithValue(@类型,输入);
如果(String.IsNullOrEmpty(单位))
{
command.Parameters.AddWithValue(@单位,DBNull.Value);
}
,否则
command.Parameters.AddWithValue(@单位,单位);
如果(String.IsNullOrEmpty(范围))
{
command.Parameters.AddWithValue(@范围,DBNull.Value);
}
,否则
command.Parameters.AddWithValue(@范围,范围);
如果(String.IsNullOrEmpty(规模))
{
command.Parameters.AddWithValue(@秤,DBNull.Value);
}
,否则
command.Parameters.AddWithValue(@规模,规模);
如果(String.IsNullOrEmpty(介绍))
{
command.Parameters.AddWithValue(@描述,DBNull.Value);
}
,否则
command.Parameters.AddWithValue(@描述,描述);
command.Parameters.AddWithValue(@ GUID,GUID)
回报(INT)command.ExecuteScalar();
}
}
解决方案试试这个代码:
的SqlParameter unitsParam = command.Parameters.AddWithValue(@单位,单位);
如果(单位== NULL)
{
unitsParam.Value = DBNull.Value;
}
和则必须检查null值的所有其它参数。如果空你必须通过
DBNull.Value
值。I'm getting this exception:
The parameterized query '(@Name nvarchar(8),@type nvarchar(8),@units nvarchar(4000),@rang' expects the parameter '@units', which was not supplied.
My code for inserting is:
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(); } }
The exception was a surprise because I'm using the
AddWithValue
function and making sure I added a default parameters for the function.SOLVED:
The problem was that the some parameters where empty Strings (that override the default)
This is the working code:
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(); } }
解决方案Try this code:
SqlParameter unitsParam = command.Parameters.AddWithValue("@units", units); if (units == null) { unitsParam.Value = DBNull.Value; }
And you must check all other parameters for null value. If it null you must pass
DBNull.Value
value.这篇关于该参数化查询.....预计参数'@units',但未提供该的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!