3

I just found in some code that it uses System.Data.SqlTypes.SqlString? I haven't seen it used before. Am I always supposed to use this when sending a string to sql server?

e.g.

cmd.Parameters.Add("@FirstName", (SqlString)FirstName);

4 Answers 4

7

No, it's fine - you can just use string and the framework/driver will take care of things for you.

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

Comments

2

I dont think you have to as it will figure it out itself but I always at least say the type just to ensure that it goes through correctly.

Comments

0

I'm sure there are conversions. It should at least be

new SqlString(FirstName)

there to avoid trouble. SqlString mainly exists to fit in the hierarchy of DbTypes (implementing INullable e.g.)

Comments

0

It's fine to just pass a normal string. Nothing wrong with converting to a SqlString though; it would be better to call one of the constructors directly though - it can be useful if you want to check locale conversion client side.

I should point out that that method is deprecated and you should be using AddWithValue or specifying the field type, and setting the value after.

Comments

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.