Requires DBNull.Value for types with a null value in the form SqlCommandParameter.Value

Which approach is recommended:

void Add(int? value)
{
   command.Parameters.Add("@foo").Value = value;
}

or

void Add(int? value)
{
   command.Parameters.Add("@foo").Value = (object)value ?? DBNull.Value;
}
+3
source share
2 answers

If you want to pass null to a parameter, you need to pass DBNull.Value, as in your second example.

If you assign nulla parameter as the value, this parameter will not be sent to the procedure, which will cause the procedure to fail (if the parameter is required) or use the default value for the parameter, which may or may not be zero.

+4
source

It depends on the functionality you want.

null , SqlCommand , . , , , .

DBNull.Value, SqlCommand SQL-.

+1

Source: https://habr.com/ru/post/1734668/


All Articles