The parameterized query … expects the parameter '@units', which was not supplied

匿名 (未验证) 提交于 2019-12-03 01:52:01

问题:

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();         }       } 

回答1:

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.



回答2:

Here's a way using the null-coalescing operator:

cmd.Parameters.AddWithValue("@units", units ?? (object)DBNull.Value); cmd.Parameters.AddWithValue("@range", range ?? (object)DBNull.Value); cmd.Parameters.AddWithValue("@scale", scale ?? (object)DBNull.Value); cmd.Parameters.AddWithValue("@description", description ?? (object)DBNull.Value); 

Or for more strict type checking:

cmd.Parameters.Add("@units", SqlDbType.Int).Value = units ?? (object)DBNull.Value; cmd.Parameters.Add("@range", SqlDbType.Int).Value = range ?? (object)DBNull.Value; cmd.Parameters.Add("@scale", SqlDbType.Int).Value = scale ?? (object)DBNull.Value; cmd.Parameters.Add("@description", SqlDbType.VarChar).Value = description ?? (object)DBNull.Value; 

The operator also be chained:

int?[] a = { null, null, 1 }; Console.WriteLine(a[0] ?? a[1] ?? a[2]); 


回答3:

This is a method to be reused with multiple parameters:

    public void NullorEmptyParameter(QC.SqlCommand command, string at, string value)     {         if (String.IsNullOrEmpty(value))         {             command.Parameters.AddWithValue(at, DBNull.Value);         }         else             command.Parameters.AddWithValue(at, value);     } 

And then you can reuse it for as many commands and params:

    NullorEmptyParameter(command, "@Idea_ID", Idea_ID);     NullorEmptyParameter(command, "@Opportunities_or_Idea", Opportunities_or_Idea); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!