SQL Data Reader - handling Null column values

前端 未结 27 2191
甜味超标
甜味超标 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:09

    I don't think there's a NULL column value, when rows are returned within a datareader using the column name.

    If you do datareader["columnName"].ToString(); it will always give you a value that can be a empty string (String.Empty if you need to compare).

    I would use the following and wouldn't worry too much:

    employee.FirstName = sqlreader["columnNameForFirstName"].ToString();
    

提交回复
热议问题