Code: Select all
PgSqlCommand command = new PgSqlCommand("SELECT NULL", connection); PgSqlDataReader reader = command.ExecuteReader(); reader.Read(); bool isNull = reader.IsDBNull(0); // returns true int value = reader.GetInt32(0); // returns 0
Most of my data is never expected to contain NULL values, so it is actually an exceptional case if I see one--I could write a wrapper around your code to verify IsDBNull=false and throw if it isn't on every column I expect to always have a value, but in addition to making my program silently accept bad data, the conversion you're performing is also a violation of the SQL standard.
The standard states that NULL represents an unknown value, which is why NULL = NULL is NULL instead of TRUE and the IS NULL check exists. The choice of conversion to 0 isn't any more valid than conversion to -1 or 12. In some cases, 0 is not an acceptable value for a column, and your conversion can lead to bugs far down the road from the initial insertion of the value.