How do I set my Oracle session's globalization to be the same as Windows in .NET?

佐手、 提交于 2019-12-24 03:24:20

问题


For example, it would be great if I could do the following:

private void SetSessionGlobalization(Oracle.DataAccess.Client.OracleConnection aConnection)
{
    System.Globalization.CultureInfo lCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
    aConnection.SetSessionInfo(lCultureInfo);
}

But that does not work because SetSessionInfo takes a OracleGlobalization class as a parameter, not a CultureInfo!

And this does not work either:

private void SetSessionGlobalization(Oracle.DataAccess.Client.OracleConnection aConnection)
{
    Oracle.DataAccess.Client.OracleGlobalization lClientGlobalization = Oracle.DataAccess.Client.OracleGlobalization.GetClientInfo());
    aConnection.SetSessionInfo(lClientGlobalization);
}

Because GetClientInfo gets Oracle's version of the client globalization settings, not Windows.

What am I missing here? How do I set my database connection session to be the same as that used by my thread (which will be the same as Windows by default)?


回答1:


I think you have to assign each property manually. It would be like this.

private void SetSessionGlobalization(Oracle.DataAccess.Client.OracleConnection aConnection)
{

     OracleGlobalization info = aConnection.GetSessionInfo();
     System.Globalization.CultureInfo lCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
     var ri = new System.Globalization.RegionInfo(lCultureInfo.LCID);

     info.Calendar = lCultureInfo.Calendar.GetType().Name.Replace("Calendar", String.Empty);         
     info.Currency = ri.CurrencySymbol;
     info.DualCurrency = ri.CurrencySymbol;
     info.ISOCurrency = ri.ISOCurrencySymbol;
     info.DateFormat = lCultureInfo.DateTimeFormat.ShortDatePattern + " " + lCultureInfo.DateTimeFormat.ShortTimePattern.Replace("HH", "HH24").Replace("mm", "mi");
     info.DateLanguage = System.Text.RegularExpressions.Regex.Replace(lCultureInfo.EnglishName , @" \(.+\)",String.Empty);
     info.NumericCharacters = lCultureInfo.NumberFormat.NumberDecimalSeparator + lCultureInfo.NumberFormat.NumberGroupSeparator;
     info.TimeZone = String.Format("{0}:{1}", TimeZoneInfo.Local.BaseUtcOffset.Hours,  TimeZoneInfo.Local.BaseUtcOffset.Minutes);
     info.Language = ...
     info.Territory = ...
     info.TimeStampFormat = ...
     info.TimeStampTZFormat = ...

     try {
        aConnection.SetSessionInfo(info);
     } catch ( OracleException err ) {
        MessageBox.Show(err.Message);
     }
}

You have to to several kind of translations, e.g. for Date format or Region/Language. I hope you got an idea how to do it.

Note some (important) settings (e.g. ClientCharacterSet) are Read/only, these values are derived from Registry or Environment variables and are set while opening the connection.

So, the preferred way is to use Registry or Environment variables, then you don't have to set OracleGlobalization.



来源:https://stackoverflow.com/questions/29388534/how-do-i-set-my-oracle-sessions-globalization-to-be-the-same-as-windows-in-net

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