Joe
Joe

Reputation: 744

SqlString in c#

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);

Upvotes: 3

Views: 3363

Answers (4)

Rowland Shaw
Rowland Shaw

Reputation: 38130

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.

Upvotes: 0

sehe
sehe

Reputation: 392911

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.)

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500145

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

Upvotes: 7

Richard Adnams
Richard Adnams

Reputation: 3128

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.

Upvotes: 2

Related Questions