0

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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

1 Comment

@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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.