using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SinoChemCommonUtilities { public static class DbFieldReader { public static T SafelyReadDbField(object rawFieldValue) { if (!DBNull.Value.Equals(rawFieldValue)) { return (T)Convert.ChangeType(rawFieldValue, typeof(T)); } else // return default value for each type if db null { if (typeof(T) == typeof(string)) { return (T)Convert.ChangeType("", typeof(string)); } else if (typeof(T) == typeof(DateTime)) { return (T)Convert.ChangeType(DateTime.Now, typeof(DateTime)); } else { return default(T); } } } } }