DBNull is a native class that contains a singleton in the Value property. DBNull.Value is not null, as this is a reference to an instance of this class.
In most cases, if not all database wrappers return DBNull.Value instead of null when there is no value. One reason for this is that returning real null may mean that it does not have a row, not just a null value in the column (this depends on what objects you use to get the values).
As a rule, the as operator is very useful with DBNull and can be used with any type of NULL (including string ).
string str = reader["Name"] as string; int? i = reader["Age"] as int?;
It can also be noted that the operator ? also very useful here when you need a value type that is not nullable (although it doesnโt look too pretty).
int i = reader["Age"] as int? ?? -1;
I find this very convenient as a small script anyway in the select LINQ clause.
source share