Do I need to specify the SQL datatype when using SqlCommand?

Is there any provable reason why I should always specify the SQL datatype for SqlCommand string keys?

0


source to share


3 answers


The only time I ran into a situation where I needed to specify the datatype was when passing in DBNull. When I didn't specify the datatype it defaulted to Varchar and ended up crashing because I was trying to set an integer value to Null.



+1


source


command.AddWithValue ("@Id", "1"); // Id is an int

command .AddWithValue ("@Id", '1'); // Id is an int



Do you guys know if there is a difference between "and" when we use SqlCommand internally? I have google for a long time, but I haven't seen anything about this problem.

+1


source


and you will find that sometimes you will get very strange errors if you don't specify sql type and size

it's safer and more self-documenting - always declare the correct sql type and size

0


source







All Articles