Neil Knight
Neil Knight

Reputation: 48547

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?

Upvotes: 27

Views: 22522

Answers (4)

Filip De Vos
Filip De Vos

Reputation: 11908

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.

Upvotes: 8

Pranay Rana
Pranay Rana

Reputation: 176936

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

Upvotes: 2

Mark Dickinson
Mark Dickinson

Reputation: 6633

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

Upvotes: 2

BrokenGlass
BrokenGlass

Reputation: 160942

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.

Upvotes: 35

Related Questions