Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have the following table in Oracle 11g:

SQL
create table tb_dates ( dt_date date )


And the following code in C# 4.5:

SQL
DataTable dt = new DataTable();
DataColumn dc = new DataColumn();
dc.DataType = Type.GetType("System.DateTime");
dc.ColumnName = "dt_date";
dt.Columns.Add(dc);
for (int i = 0; i < 5; i++)
{
    DataRow dr = dt.NewRow();
    dr["dt_date"] = System.DateTime.Now;
    dt.Rows.Add(dr);
}

using (OracleBulkCopy bulkCopy = new OracleBulkCopy("data source=orcl;user id=scott;password=tiger", OracleBulkCopyOptions.Default))
{
    bulkCopy.DestinationTableName = "tb_dates";
    bulkCopy.WriteToServer(dt);
}


The dates in the table must be 2014/05/15. But, when I do the select in Oracle, I have 1420/05/15. The year has been reversed.

I'm using Oracle 11g and the Oracle ODP.NET 12 client. What I have to do for this code work correctly?
Posted
Updated 20-May-14 9:16am
v2
Comments
Vedat Ozan Oner 20-May-14 15:37pm    
can you tell me what you get when you run select to_char(dt_date, 'YY') from tb_dates
JimVas2005 21-May-14 17:15pm    
I get: 20
Vedat Ozan Oner 22-May-14 3:34am    
oops, that is bad. because that means you are inserting year 1420 actually, not 2014 as expected. that is weird since this conversion is handled by odp.net driver. you can try to insert one by one instead of bulk insert to see what happens when you do so. another option, you can set datetime value as formatted string like dr["dt_date"] = System.DateTime.Now.ToString("yy/MM/dd HH:mm:ss"); assuming that your oracle default datetime format as in the date format string. but that is really weird.
JimVas2005 22-May-14 12:19pm    
When I execute this code:
string conexionDestino = "data source=orcl;user id=scott;password=tiger";
using (OracleConnection connection = new OracleConnection(conexionDestino))
using (OracleCommand cmd = new OracleCommand("insert into tb_dates values (:fecha)", connection))
{
connection.Open();
DateTime fecha = DateTime.Now;
cmd.Parameters.Add("fecha", fecha);
int rc = cmd.ExecuteNonQuery();
}
The date in the table is 22/05/2014; the correct date
What is the difference between the bulkcopy and the direct insert?
Vedat Ozan Oner 22-May-14 12:33pm    
Simply I don't know. But I think you have just found a bug in odp.net. Congradulations :) you should report it to the odp.net development team. Before reporting it would be better to check if there exists any option about date time format for bulk insert.

1 solution

have problem also.
you can simply change halves of year in code.
C#
foreach (DataColumn dc in dataTable.Columns)
{
    if (dc.DataType == typeof(DateTime))
    {
        foreach (DataRow dr in dataTable.Rows)
        {
            object value = dr[dc];
            if (!Convert.IsDBNull(value))
            {
                DateTime d = (DateTime)dr[dc];
                int year;
                int century = Math.DivRem(d.Year, 100, out year);
                dr[dc] = new DateTime(year * 100 + century, d.Month, d.Day);
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900