Search code examples
c#.netasp.net-mvcc#-4.0oracle-sqldeveloper

Add Date in Oracle Table for TimeStamp Column using c#


I have written Code to add the Date to oracle table with column of type 'TimeStamp' but i am getting the error of

'ORA-01843: not a valid month', my code is below, column in oracle DB of type 'Timestamp', trying to update the column with c#,

DateTime dt = 
        DateTime.Parse(Convert.ToString(CurrentItem[SharePointColumnInternal[j]]));
        dt = dt.ToLocalTime();
        UpdateCmd += i + "=" + "'" + dt + "'" + ",";  

This is the update command string that I have built :UpdateCmd and executing in the c# code.


Solution

  • You shouldn't be building your SQL commands by concatenating strings; this is not only more difficult, but also open to SQL injection.

    Have a look at using parameterised queries, for example:

    DateTime dt = DateTime.Parse(Convert.ToString(CurrentItem[SharePointColumnInternal[j]]));
    dt = dt.ToLocalTime();
    
    using (var connection = new OracleConnection("YourConnectionString"))
    using (var command = new OracleCommand("UPDATE YourTable SET YourDateTimeColumn = :dt Where ...", connection))
    {
        command.Parameters.Add("dt", dt);
        command.ExecuteNonQuery();
    }