c# - Trying to insert DateTime.Now into Date/Time field gives "Data type mismatch" error -
if try write datetime record in ms-access database easy way, this
cmd.commandtext = "insert [table] ([date]) values (?)"; cmd.parameters.addwithvalue("?", datetime.now);
i exception saying "data type mismatch in criteria expression."
can tell me why? goes wrong here?
after little experimentation, found can make work if write
oledbparameter parm = new oledbparameter("?", oledbtype.date); parm.value = datetime.now; cmd.parameters.add(parm);
but doing seems less neat, less straightforward. why necessary? overlooking simple?
the problem of mismatch in criteria expression due oledbtype assigned parameter used represent datetime.now value when call addwithvalue
.
the oledbtype choosen addwithvalue dbtimestamp
, access wants oledbtype.date
.
http://support.microsoft.com/kb/320435
searching on net have found intersting tip. core problem lies in oledbparameter cannot handle milliseconds part of datetime.now. forcing oledbtype date milliseconds part omitted. have found insert works dbtimestamp type if remove milliseconds date.
cmd.parameters.addwithvalue("?", getdatewithoutmilliseconds(datetime.now)); private datetime getdatewithoutmilliseconds(datetime d) { return new datetime(d.year, d.month, d.day, d.hour, d.minute, d.second); }
oh, well, waiting explain better.