SqlCommand Parameters size confusion

Neil Knight picture Neil Knight · Feb 28, 2012 · Viewed 17.9k times · Source

I have the following line of code:

sqlcommand.Parameters.Add("@LinkID", SqlDbType.Int, 4).Value = linkID;

But, I'm slightly confused about the use of size. Is this saying that its 4 bytes in size? Or a length of 4 so 1234 is acceptable but 12345 is too big?

Answer

BrokenGlass picture BrokenGlass · Feb 28, 2012

For the types with fixes size you should omit this argument, simply:

sqlcommand.Parameters.Add("@LinkID", SqlDbType.Int).Value = linkID;

The size argument is only relevant for parameters with a type that can have variable size like varchar, nvarchar etc.