Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlCommand Parameters size confusion

Tags:

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?

like image 698
Neil Knight Avatar asked Feb 28 '12 14:02

Neil Knight


People also ask

What is SqlCommand parameters AddWithValue?

AddWithValue replaces the SqlParameterCollection. Add method that takes a String and an Object. The overload of Add that takes a string and an object was deprecated because of possible ambiguity with the SqlParameterCollection.

Which object is used to pass variable to SqlCommand?

Command objects use parameters to pass values to SQL statements or stored procedures, providing type checking and validation. Unlike command text, parameter input is treated as a literal value, not as executable code.

What is SqlDbType VarChar?

A variable-length stream of non-Unicode characters ranging between 1 and 8,000 characters. Use VarChar when the database column is varchar(max) . Variant.

What is SqlDbType in C#?

SqlDbType: It is used to set the SQL Server Datatypes for a given parameter. ParameterName: It is used to specify a parameter name. Direction: It is used for setting the direction of a SqlParameter. It is Input or Output or both (InputOutput).


1 Answers

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.

like image 73
BrokenGlass Avatar answered Oct 13 '22 13:10

BrokenGlass