JTM February 2016

Filling data from an oracle table is not taking date and time

I am trying to fill a datatable with data from an oracle table. While fetching the data I uses oracle connection, and Oracle data adapter in c# to fill a data table. The actual oracle table has a column with datatype date .This column has both date and time in this format "10/26/2006 3:52:18 PM' ,but when it is filled to a datatable it takes only the date .Please help

Here is my code ,

string oquery = "SELECT * FROM table_NAME WHERE INSERT_DATE between to_date ('" + startdate[i] + "', 'yyyy-mm-dd') AND to_date ('" + enddaterr[i] + "', 'yyyy-mm-dd') ";

                OracleCommand cmd = new OracleCommand(oquery, con);
                OracleDataAdapter oda = new OracleDataAdapter(cmd);
                DataTable dtb = new DataTable();
                oda.Fill(dtb);

Answers


Ggalla1779 February 2016

Have you tried casting you date in C#

DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt")

And when retrieving it from Oracle cast it back, using Oracle format

SELECT TO_CHAR(sysdate,'MM/dd/yyyy hh:mi:ss AM') from dual

using (OracleDataReader dr =  Zurich.Common.OracleDataAccess.ExecuteReader(cn, CommandType.Text, mySQL))
        {
            if (dr.HasRows) // file exists in DB
            {
                while (dr.Read())
                {
                    currentDays = Convert.ToInt16(((DateTime.Now.AddDays(-1).Date) - Convert.ToDateTime(dr[0])).TotalDays);
                    Console.WriteLine(CheckDate(Convert.ToDateTime(dr[0])));

                    lbBugs.Items.Add(String.Format("{0:dd/M/yyyy H:mm:ss}", dr[0]) + ": " + dr.GetString(1) + " : " + currentDays);

Post Status

Asked in February 2016
Viewed 2,151 times
Voted 5
Answered 1 times

Search




Leave an answer