Handle DBNull in C#

前端 未结 13 663
梦谈多话
梦谈多话 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:20

    The way I handle this is

    int? stockvalue = reader["StockValue"] as int?;
    

    Very simple, clean and one line. If for some reason I absolutely can't have a null value (which I find poor reasoning for usually since I'd rather know if a value has meaning or if it was unitialized for a primitive type) I would do:

    int stockvalue = (reader["StockValue"] as int?).GetValueOrDefault(-1);
    
    0 讨论(0)
  • 2020-11-30 02:20

    Yes you can use int? This way you can have a default value of null instead of 0. Since the result of stockvalue could potentially be 0 there isn't confusion as to whether the database was 0 or null. For instance like this (pre nullable) we had a default initialization of -1 to represent no value was assigned. Personally, I thought this was a little dangerous because if you forget to set it to -1, there is a data corruption issue that can be really difficult to track down.

    http://msdn.microsoft.com/en-us/library/2cf62fcy(VS.80).aspx

    int? stockvalue = null;
    
    if (!Convert.IsDBNull(reader["StockValue"]))
        stockvalue = (int)reader["StockValue"];
    
    //Then you can check 
    
    if(stockValue.HasValue)
    {
      // do something here.
    }
    
    0 讨论(0)
  • 2020-11-30 02:24

    The shortest (IMHO) is:

    int stockvalue = (reader["StockValue"] as int?) ?? 0;
    

    Explanation:

    • If reader["StockValue"] is of type int, the value will be returned, and the "??" operator will return the result
    • If reader["StockValue"] is NOT of type int (e.g. DBNull), null will be returned, and the "??" operator will return the value 0 (zero).
    0 讨论(0)
  • 2020-11-30 02:24
    int? stockValue = reader["StockValue"] == null || reader["StockValue"] == DBNull.Value ? null : (int?)reader["StockValue"];
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 02:32

    While it's convenient to reference reader["StockValue"], it's not very efficient. It's also not strongly-typed, as it returns type object.

    Instead, within your code, do something like this:

    int stockValueOrdinal = reader.GetOrdinal("StockValue");
    int? stockValue = reader.IsDbNull(stockValueOrdinal) ?
        null : 
        reader.GetInt32(stockValueOrdinal);
    

    Of course, it's best to get all of the ordinals at one time, then use them throughout the code.

    0 讨论(0)
提交回复
热议问题