c# - SQL error converting data type datetime to smalldatetime? -
in sql stored procedure:
@startdate smalldatetime, @enddate smalldatetime
in c# code:
startdate = new datetime(1901,01,01,00,00,00); enddate = new datetime(2200,01,01,00,00,00); var startdate2 = startdate.tostring("dd/mm/yyyy hh:mm:ss"); startdate2 = datetime.parseexact(startdate2, "dd/mm/yyyy hh:mm:ss", cultureinfo.invariantculture).tostring("dd/mm/yyyy hh:mm:ss"); startdate = convert.todatetime(startdate2); var enddate2 = enddate.tostring("dd/mm/yyyy hh:mm:ss"); enddate2 = datetime.parseexact(enddate2, "dd/mm/yyyy hh:mm:ss", cultureinfo.invariantculture).tostring("dd/mm/yyyy hh:mm:ss"); enddate = convert.todatetime(enddate2);
the column smalldatetime type in db. i'm going round in circles this! whats correct way parse dates correct format querying in db?
update: changed code , worked fine.
sqlparameter parameter = daholiday.selectcommand.parameters.add("@startdate", system.data.sqldbtype.smalldatetime); sqlparameter parameter2 = daholiday.selectcommand.parameters.add("@enddate", system.data.sqldbtype.smalldatetime); parameter.value = startdate; parameter2.value = enddate;
don't convert them string, pass them datetime
type object through command parameters.
datetime
should never converted string match format of data in database table. if datatype in table datetime
better pass .net framework datetime
object parameter command.
Comments
Post a Comment