How to check if Datarow value is null

后端 未结 2 1070
南方客
南方客 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

    Check if the data column is not null with DataRow.IsNull(string columnName)

    if (!row.IsNull("Int64_id"))
    {
      // here you can use it safety
       long someValue = (long)row["Int64_id"];
    }
    
    0 讨论(0)
  • 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<T>(this DataRow row, string columnName)
        {
          return row.IsNull(columnName) ? default(T) : row.Field<T>(columnName);
        }
      }
    

    You can use is as follows:

    int id = dataRow.FieldOrDefault<int>("Id");
    
    0 讨论(0)
提交回复
热议问题