I am trying to insert a .NET DateTime value into an Oracle DATE column using ODP.NET. Thus far, I have tried using the OracleDate() constructor to cast the .NET DateTime like so:
new OracleDate(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTimeNow.Hour, DateTime.Now.Minute, DateTime.Now.Second)
When I try this, it inserts the correct year, month, and day into the column but the time is always set to midnight. How do I insert the correct time along with the date?
Without parameters, the SQL looks like this (DateTime.Now is used for clarity, otherwise I’d just use SYSDATE):
"update mytable set timestamp = '" + new OracleTimeStamp(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTimeNow.Second) + "'"
According to this you should use
OracleTimeStampasOracleDbTypeto achieve what you want…OracleDateis without time portion… this behaviour started sometime ago (IIRC Oracle 8 or so) with the JDBC drivers… with the JDBC drivers there was/is some weird workaround which I don’t remember it right now… I don’t know if there is a workaround for ODP.NET…OracleTimeStampis the officially supported way for dates with time portions…EDIT – after OP added SQL statement:
First of all this statement contains two problems – never name a column/table like a reserved word (i.e.
timestamp)… the other problem is the lack of using a parameter which in this case won’t lead to SQL injection but still is bad practice and leads (if this same statement is used multiple times) to a minimal loss of performance…IF you still want to use it that way THEN this will work: