suhas
suhas

Reputation: 173

How to convert c# datetime into oracle timestamp?

I am trying to store c# datetime into oracle database as timestamp it is storing without problems, but when i am trying to select data from database for that particular date it is giving me error as not valid month..

DateTime sessiondate =DateTime.Now();
int userid_int = 101;
string userTypeis ='ENGINEER';

string get_Log_query = "SELECT LOG_ID FROM JOINTING_LOGIN_LOG where USER_ID="+userid_int+" and USER_TYPE='"+userTypeis+"' and SESSION_START='"+sessiondate+"'";

Upvotes: 2

Views: 8249

Answers (1)

Stawros
Stawros

Reputation: 935

string get_Log_query = "SELECT LOG_ID FROM JOINTING_LOGIN_LOG where USER_ID="+userid_int+" and USER_TYPE='"+userTypeis+"' and SESSION_START='"+sessiondate+"'";

You should never use string concatenation to building queries with parameters. Parameterized queries will be faster and help you to avoid SQL Injection and troubles with type conversion. If SESSION_START is really timestamp, your code will be -

string get_Log_query = 
    @"SELECT LOG_ID 
        FROM JOINTING_LOGIN_LOG 
       WHERE USER_ID= :USER_ID 
         and USER_TYPE= :USER_TYPE 
         and SESSION_START= :SESSION_START";

 cmd.Parameters.Add("USER_ID", OracleDbType.Decimal, userid_int, ParameterDirection.Input);
 cmd.Parameters.Add("USER_TYPE", OracleDbType.Varchar2, userTypeis, ParameterDirection.Input);
 cmd.Parameters.Add("SESSION_START", OracleDbType.TimeStamp, sessiondate, ParameterDirection.Input);

Upvotes: 6

Related Questions