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;
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you want to pass a null value in a parameter, you need to pass
DBNull.Valueas in your second example.If you assign
nullas 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.