I am using the following SQL query and the ExecuteScalar()
method to fetch data from an Oracle database:
sql = \"select username from usermst wh
SQL NULL value
IF ( value IS NULL )
if (obj == DBNull.Value)
{}
Best practice when reading from a data reader:
var reader = cmd.ExecuteReader();
...
var result = (reader[i] == DBNull.Value ? "" : reader[i].ToString());
In my experience, there are some cases the returned value can be missing and thus execution fails by returning null. An example would be
select MAX(ID) from where
The above script cannot find anything to find a MAX in. So it fails. In these such cases we must compare the old fashion way (compare with C# null
)
var obj = cmd.ExecuteScalar();
var result = (obj == null ? -1 : Convert.ToInt32(obj));
- 热议问题