Handle DBNull in C#

前端 未结 13 756
梦谈多话
梦谈多话 2020-11-30 01:36

Is there a better/cleaner way to do this?

int stockvalue = 0;
if (!Convert.IsDBNull(reader[\"StockValue\"]))
    stockvalue = (int)reader[\"StockValue\"];
         


        
13条回答
  •  一整个雨季
    2020-11-30 02:29

    You could do this conversion directly in your DB-query, thus avoiding the special case alltogether.

    But I wouldn't call that 'cleaner', unless you can consistently use that form in your code, since you would lose information by returning '0' instead of NULL from the DB.

提交回复
热议问题