There are several approaches here. Unfortunately, DBNull makes this unpleasant - the API reader[name] returns an object , which can be your value, or it can be DBNull.Value - so you need to check this ( is ) and process it. Another approach is to use the reader.IsDBNull(ordinal) API, but as you will notice: this requires a sequence number (column index), not a name. In any case, you can add things like utility methods to help:
static object Read(IDataReader reader, string name) { var val = reader[name]; return val is DBNull ? (object)null : val; }
then (for example):
mb.Mem_ResPin = (int?)Read(reader, "Mem_ResPin")
However, again: tools like dapper can make this a lot easier for you; one Query<T>(tsql, args).SingleOrDefault() will deal with all of this, including nulls, Nullable<T> and a number of other scripts.
source share