SqlCommand Parameters size confusion
Asked Answered
M

4

27

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?

Muir answered 28/2, 2012 at 14:2 Comment(0)
H
34

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.

Heehaw answered 28/2, 2012 at 14:5 Comment(1)
So... should you pass the size of the LinkID or the maximum size of the column?Enthronement
D
8

The size is 4 bytes for an int.

See DbParameter class on msdn for more info. It is relevant because SqlCeParameter implements DbParameter

The following section is relevant:

The Size property is used for binary and string types.

For nonstring data types and ANSI string data, the Size property refers to the number of bytes. For Unicode string data, Size refers to the number of characters. The count for strings does not include the terminating character.

For variable-length data types, Size describes the maximum amount of data to transmit to the server. For example, for a Unicode string value, Size could be used to limit the amount of data sent to the server to the first one hundred characters.

See this https://gist.github.com/1932766 for the implementation of the Size property.

Dominicadominical answered 28/2, 2012 at 14:4 Comment(0)
T
2

It is 4 bytes, 32 bits. It is a 32 bit integer.

Theseus answered 28/2, 2012 at 14:5 Comment(0)
G
2

if you are going for int than i think therre is no matter what size of it.

so you code will be

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

on for varchar,navarchar where the size is maater you need to speicify size in you .net code i.e in parameter

Gory answered 28/2, 2012 at 14:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.