本文介绍了C#连接到Oracle DB DateTime格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到Oracle后端的.Net webapp。
我有一个基页,每页使用我设置我的

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

//创建特定于我们的应用程序的DateTime信息。
System.Globalization.DateTimeFormatInfo dateTimeInfo =
new System.Globalization.DateTimeFormatInfo();

//定义各种日期和时间格式。
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;

//设置应用程序的日期时间格式。
cultureInfo.DateTimeFormat = dateTimeInfo;

//将我们的定制文化分配给应用程序。
//Application.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
base.OnPreInit(e);
}

在我的应用程序中,我使用OracleDataAdapter在数据库上执行纯文本查询。
我正在过滤这样的日期

 MyDateColumn= '01 -Jan-2000'

这在本地工作正常。但是,当我到达服务器时,在我的过滤器中工作的唯一日期格式为

 MyDateColumn='2000 Jan 01'

我缺少什么?

解决方案

您应该总是将 DATE 列明确比较为DATE值,即:

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

不要依赖隐式日期转换。


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?

解决方案

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.

这篇关于C#连接到Oracle DB DateTime格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:43