tags:

views:

120

answers:

3

Is there any provable reason why I should always specify the SQL data type for SqlCommand paramenters?

+1  A: 

The only time I've run into a case when I had to specify a data type was when passing in DBNull. When I wasn't specifying a data type, it defaulted to a Varchar, and ended up crashing because I was trying to set an integer value to Null.

Kibbee
A: 

and you will find that sometimes you will get very strange errors when you have not specified the sql type and size

it is safest - and more self-documenting - to always declare the correct sql type and size

Steven A. Lowe
Can you give me an example?
Jonathan Allen
@Grauenwolf: there was a 'wierd SQL error' SO post ~ a week ago caused by not specifying the length of a varchar, but i searched and could not find it. so no, i can't give you a specific example but i can assure you that it does happen. Let me ask you: why would you trust/assume w/out specifying?
Steven A. Lowe
+1  A: 

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

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

Do you guys know there is any difference between " " and ' ' when we use inside SqlCommand? I have google for a long time but I didn't see anything about this trouble.