SQL Data Reader - handling Null column values

前端 未结 27 2486
甜味超标
甜味超标 2020-11-22 08:53

I\'m using a SQLdatareader to build POCOs from a database. The code works except when it encounters a null value in the database. For example, if the FirstName column in the

27条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 09:13

    As an addition to the answer by marc_s, you can use a more generic extension method to get values from the SqlDataReader:

    public static T SafeGet(this SqlDataReader reader, int col)
        {
            return reader.IsDBNull(col) ? default(T) : reader.GetFieldValue(col);
        }
    

提交回复
热议问题