Best practice to check if DataRow contains a certain column

后端 未结 5 2016
无人及你
无人及你 2020-12-13 17:28

At the moment, when I iterate over the DataRow instances, I do this.

foreach(DataRow row in table)
  return yield new Thingy { Name = row[\"hazaa\"]         


        
5条回答
  •  一个人的身影
    2020-12-13 17:56

    Sometimes a column name might exist, but a row does not contain the data for that column; for example, after filling DataTable using ReadXML.

    A simple, fast and secure solution would be to use type checking:

    if(row["columnname"].GetType() != typeof(System.DBNull)){
        //DataRow contains "columname"
    }else{
        //a safe scope to set default cell data
    }
    

提交回复
热议问题