简体   繁体   中英

Updating Nulls

I am trying to generate a data layer template. When I do my Selects, Updates, and Inserts, the idea is to have the template work with all the columns because I don't know which one's contain values or not. The problem is that I may have an update statemtent like cmd.Parameters.AddWithValue("@Field", this.Field); and if that value is null, the query won't execute. How can I get around this problem?

UPDATE:

I tried the ?? solution but I receive the error Operator ?? cannot be applied to operands string(or int) and System.DBNull. It seems to only work if the field is actually null, but not if it has a value. Then I tried to place the type (object)DBNull in front of DBNull but still nothing.

Adding (object) to this field worked!

Thanks.

cmd.Parameters.AddWithValue("@Field", this.Field ?? DBNull.Value);

?? is the coalesce operator in C#.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM