Is there a difference between DataReader[0].ToString()
and (string)DataReader[0]
?
My guess is that (string)DataReader[0]
might fail if the database type isn't a string type, where DataReader[0].ToString()
would just convert anything outside of a DB null to a string. Is that the case?
Which would be faster?
Those both introduce you to potential data exceptions, IMO the optimal way to read from a reader is:
var x = reader[0] as string
Then for numbers / bools etc I always use nullable types so you can get
var y = reader[1] as int?
Now if you absolutely are as opposed to nullables for some reason (I think they're great for knowing whether something is or not set)
int i = (reader[1] as int?).GetValueOrDefault()