C# Connecting to Oracle DB DateTime formatting

百般思念 提交于 2019-12-02 01:59:22

问题


I have a .Net webapp that is connecting to an Oracle backend. I have a base page which every page uses where I set my

    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 use an OracleDataAdapter to execute plain text queries on the database. I am filtering dates like so

"MyDateColumn" = '01-Jan-2000'

This works fine on 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?


回答1:


you should always explicitely compare DATE columns to DATE values, i-e:

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

Never rely on implicit date conversion.




回答2:


Try:

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

instead of relying on implicit conversions from string to date.




回答3:


Where dt is a variable of type DateTime, try the following

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



来源:https://stackoverflow.com/questions/1534250/c-sharp-connecting-to-oracle-db-datetime-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!