The online home of John Pollard

SQL Parameters and Types

Originally posted on my old MSDN blog

I found the following page interesting when having issues setting a value in a SqlParameter constructor.

From http://msdn2.microsoft.com/en-us/library/0881fz2y.aspx:

When you specify an Object in the value parameter, the SqlDbType is inferred from the Microsoft .NET Framework type of the Object.

Use caution when you use this overload of the SqlParameter constructor to specify integer parameter values. Because this overload takes a value of type Object, you must convert the integral value to an Object type when the value is zero, as the following C# example demonstrates

Parameter = new SqlParameter("@pname", Convert.ToInt32(0));

I guess it makes sense once it's explained, but it's not immediately clear when you just use 0 rather than Convert.ToInt32(0) why it doesn't work as expected