What is the difference between null and System.DBNull.Value?

后端 未结 6 548
忘了有多久
忘了有多久 2020-11-22 11:02

Is there any difference between null and System.DBNull.Value? If yes, what is it?

I noticed this behavior now -

while (rdr.Read())
{
    if (rdr[\"I         


        
6条回答
  •  清酒与你
    2020-11-22 11:25

    DBNull.Value is annoying to have to deal with.

    I use static methods that check if it's DBNull and then return the value.

    SqlDataReader r = ...;
    String firstName = getString(r[COL_Firstname]);
    
    private static String getString(Object o) {
       if (o == DBNull.Value) return null;
       return (String) o;
    }
    

    Also, when inserting values into a DataRow, you can't use "null", you have to use DBNull.Value.

    Have two representations of "null" is a bad design for no apparent benefit.

提交回复
热议问题