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;
}
If you want to pass a null value in a parameter, you need to pass DBNull.Value
as in your second example.
If you assign null
as the parameter value, the parameter will not be sent to the procedure, which will make the procedure call to either fail (if the parameter is required) or use the default value for the parameter, which may or may not be null.