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);
No, it's fine - you can just use string
and the framework/driver will take care of things for you.
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.
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.)
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.
精彩评论