SqlCommand Parameters size confusion

2019-02-21 07:53发布

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?

4条回答
姐就是有狂的资本
2楼-- · 2019-02-21 08:00

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.

查看更多
\"骚年 ilove
3楼-- · 2019-02-21 08:00

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.

查看更多
贪生不怕死
4楼-- · 2019-02-21 08:04

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

查看更多
在下西门庆
5楼-- · 2019-02-21 08:26

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

查看更多
登录 后发表回答