You should really use parameterized queries for this and pass your DateTime object as an SQL DateTime parameter.
If you parse a DateTime into a String , you will have to deal with localization parameters on both the application server and the database server. This can lead to some unpleasant surprises, for example, to relate to the Date, as it was in the US format on the one hand and, for example, the UK on the other. Line 9/12/2000 can be either September 12th or December 9th. Therefore, when exchanging between an application and a database, save the data in a DateTime object / type.
The only time you parse a DateTime into a String is to display information (GUI). But then you have to make sure that you use the correct localization setting during parsing in order to display it in the format that the user expects.
The same principle applies to other data types, such as float . The string representation of this varies by locale, and I suppose you don't parse the float to String when passing it to the database, so why do it with DateTime
kristof
source share