SqlCommand Parameters size confusion

[亡魂溺海] 提交于 2019-12-19 12:25:43

问题


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?


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/9483319/sqlcommand-parameters-size-confusion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!