How to check if Datarow value is null

后端 未结 2 1079
南方客
南方客 2020-12-09 15:31

Tell me please is this is correct way to check NULL in DataRow if need to return a string

 Convert.ToString(row[\"Int64_id\"] ?? \"\")
         


        
2条回答
  •  不思量自难忘°
    2020-12-09 16:08

    We have created an extension class that helps in these kinds of situations.

    public static class DataRowExtensions
      {
        public static T FieldOrDefault(this DataRow row, string columnName)
        {
          return row.IsNull(columnName) ? default(T) : row.Field(columnName);
        }
      }
    

    You can use is as follows:

    int id = dataRow.FieldOrDefault("Id");
    

提交回复
热议问题