Passing DBNull.Value and Empty textbox value to database [duplicate]

南楼画角 提交于 2019-11-30 23:13:38

问题


I have some textboxes on my page which can be empty because they are optional and I have this DAL code

parameters.Add(new SqlParameter("@FirstName", FirstName));
parameters.Add(new SqlParameter("@LastName", LastName));
parameters.Add(new SqlParameter("@DisplayName", DisplayName));
parameters.Add(new SqlParameter("@BirthDate", BirthDate));
parameters.Add(new SqlParameter("@Gender", Gender));

Any of those fields can be empty. The problem is when they are empty I receive Procedure XXX requires @FirstName which was not supplied

Then I changed my code to

parameters.Add(new SqlParameter("@FirstName", String.IsNullOrEmpty(FirstName) ? DBNull.Value : (object)FirstName));
parameters.Add(new SqlParameter("@LastName", String.IsNullOrEmpty(LastName) ? DBNull.Value : (object) LastName));
parameters.Add(new SqlParameter("@DisplayName", String.IsNullOrEmpty(DisplayName) ? DBNull.Value : (object) DisplayName));
parameters.Add(new SqlParameter("@BirthDate", BirthDate.HasValue ? (object)BirthDate.Value : DBNull.Value));
parameters.Add(new SqlParameter("@Gender", String.IsNullOrEmpty(Gender) ? DBNull.Value : (object) Gender));

But this looks messy to me especially the casting to object because ternary statement requires both value to be the same type.

Why is empty string or null string not treated NULL in the database? If I have to convert this to DBNull.Value is there a cleaner way? Saving the value as empty string in the database could have helped but query for NULL in the database will get messy too

Please give your advice on common practices or something close to that.


回答1:


First, there are 2 more handy overloads:

command.Parameters.Add("@name").Value = value;

or

command.Parameters.AddWithValue("@name", value);

Personally I use the following extension method:

public static object DbNullIfNull(this object obj)
{
    return obj != null ? obj : DBNull.Value;
}

command.Parameters.AddWithValue("@name", value.DbNullIfNull());

or

public static object DbNullIfNullOrEmpty(this string str)
{
    return !String.IsNullOrEmpty(str) ? str : (object)DBNull.Value;
}



回答2:


A little re-factoring might make code less messy. Try this

dbParams.Add(SetDBNullIfEmpty("@FirstName", FirstName));
dbParams.Add(SetDBNullIfEmpty("@LastName", LastName));
dbParams.Add(SetDBNullIfEmpty("@DisplayName", DisplayName));
dbParams.Add(SetDBNullIfEmpty("@BirthDate", BirthDate));
dbParams.Add(SetDBNullIfEmpty("@Gender", Gender));

private SqlParameter SetDBNullIfEmpty(string parmName, string parmValue)
{
    return new SqlParameter(parmName, String.IsNullOrEmpty(parmValue) ? DBNull.Value : (object)parmValue));
}



回答3:


You can default the parameters in the stored procedure, making them optional.

create procedure XXX
(
   @FirstName nvarchar(50) = null,
   @LastName nvarchar(50) = null,
   ...
)


来源:https://stackoverflow.com/questions/12444115/passing-dbnull-value-and-empty-textbox-value-to-database

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