I have a db table with multiple DateTime fields with null values. They map to null DateTimes in my class.
If I try to upgrade using Dapper in my data layer:
using (IDbConnection cnn = new SqlConnection(DB.getConString())) { cnn.Open(); return cnn.Execute((this.OptionID == 0 ? _insertSQL : _updateSQL), this); }
I get a SqlDateTimeOverflow exception (because the DateTime field is '01 / 01/0001 00:00:00 ', not null.
The only way around this is to specify each parameter separately and switch the value to zero, like this:
using (IDbConnection cnn = new SqlConnection(DB.getConString())) { cnn.Open(); return cnn.Execute("UPDATE MyTable SET MyDateField = @MyDateField", new {MyDateField = (MyDateField.HasValue? MyDateField : Null), etc etc... );
I have about 50 fields in the table, so this will be very little code, plus there is an INSERT method for updating in a similar way. Is there a simpler syntax that I skip?
Simon source share