I've got a C# question. I just wanted to ask the community about the use of System.DBNull in conjunction with using a DataReader.
When querying a database and checking for null values, which is more appropriate/preferred?
Using the 'is' operator:
reader["fieldname"] is DBNull
or just checking the value:
reader["fieldname"] == DBNull.Value
Both seem to work. I just wanted to get some other opinions.
The only way to test for a DbNull value is to use IsDbNull.
The DBNull class represents a nonexistent value. In a database, for example, a column in a row of a table might not contain any data whatsoever. That is, the column is considered to not exist at all instead of merely not having a value. A DBNull object represents the nonexistent column.
If a database field has missing data, you can use the DBNull. Value property to explicitly assign a DBNull object value to the field. However, most data providers do this automatically. To evaluate database fields to determine whether their values are DBNull, you can pass the field value to the DBNull.
The System. DBNull value indicates that the Object represents missing or nonexistent data.
Given that DBNull.Value
is the only non-null value for the DBNull
class, the two are effectively equivalent. Which do you find more readable? Personally I quite like the first version, but your mileage may vary.
It's unlikely to be a problem in terms of performance either way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With