SQL NULL Value
- equivalent in C # is DBNull.Value
- if the NULLABLE column does not matter, this is what is returned
- comparison in SQL:
IF ( value IS NULL ) - comparison in C #:
if (obj == DBNull.Value) - Visually represented in C # Quick-Watch as
{}
Best practice when reading from a data reader:
var reader = cmd.ExecuteReader(); ... var result = (reader[i] == DBNull.Value ? "" : reader[i].ToString());
In my experience, in some cases, the return value may not be present and, therefore, execution fails, returning zero. An example would be
select MAX(ID) from <table name> where <impossible condition>
The above script cannot find anything to find MAX. Thus, he fails. In such cases, we must compare the old way (compare with C # null )
var obj = cmd.ExecuteScalar(); var result = (obj == null ? -1 : Convert.ToInt32(obj));
Bizhan Dec 6 '17 at 2:47 p.m. 2017-12-06 14:47
source share