C # Connecting to Oracle DB DateTime formatting

I have a .Net webapp that connects to the Oracle backend. I have a base page that each page uses where I set

    protected override void OnPreInit(EventArgs e)
{
    System.Globalization.CultureInfo cultureInfo =
        new System.Globalization.CultureInfo("en-CA");

    // Creating the DateTime Information specific to our application.
    System.Globalization.DateTimeFormatInfo dateTimeInfo =
        new System.Globalization.DateTimeFormatInfo();

    // Defining various date and time formats.
    dateTimeInfo.DateSeparator = "/";
    dateTimeInfo.LongDatePattern = "dd-MMM-yyyy";
    dateTimeInfo.ShortDatePattern = "dd-MMM-yyyy";
    dateTimeInfo.MonthDayPattern = "dd/MM";
    dateTimeInfo.LongTimePattern = "HH:mm";
    dateTimeInfo.ShortTimePattern = "HH:mm";
    dateTimeInfo.FullDateTimePattern = "dd-MMM-yyyy";

    // Setting application wide date time format.
    cultureInfo.DateTimeFormat = dateTimeInfo;

    // Assigning our custom Culture to the application.
    //Application.CurrentCulture = cultureInfo;
    Thread.CurrentThread.CurrentCulture = cultureInfo;
    Thread.CurrentThread.CurrentUICulture = cultureInfo;
    base.OnPreInit(e);
}

      

In my application, I am using OracleDataAdapter to execute simple text queries against a database. I am filtering dates as such

"MyDateColumn" = '01-Jan-2000'

      

This works well at my local. However, when I get to the server, the only dates that work in my filter are in the format

"MyDateColumn" = '2000 Jan 01'

      

What am I missing?

+2


source to share


3 answers


you should always explicitly compare columns DATE

with DATE values, ie:

"MyDateColumn" = to_date('01-Jan-2000', 'dd-Mon-yyyy')

      



Never rely on implicit date conversion.

+3


source


Try:

"MyDateColumn" = to_date('01-Jan-2000','DD-MON-YYYY')



instead of relying on implicit conversions from string to date.

+1


source


Where dt is a variable of type DateTime try this

to_date ('"+ dt.ToString (" MM / dd / yyyy HH: mm: ss ") +"', 'MM / dd / yyyy HH: mi: ss')

+1


source







All Articles