Search code examples
c#sqlparameter

SqlCommand Parameters size confusion


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?


Solution

  • 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.