.NET Core 2 + System.Data.OracleClient.汉字不起作用 [英] .NET Core 2 + System.Data.OracleClient. Chinese characters doesn't work

查看:350
本文介绍了.NET Core 2 + System.Data.OracleClient.汉字不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将几周前发布的System.Data.OracleClient软件包与.NET Core 2结合使用: https://www.nuget.org/packages/System.Data.OracleClient/

I'm using .NET Core 2 with the System.Data.OracleClient package published some weeks ago here: https://www.nuget.org/packages/System.Data.OracleClient/

我可以阅读数字,日期和普通的英文字符.但是不是中文.可能还有很多其他非西方字符.

I can read numbers, dates and normal English characters. But not Chinese. Probably a lot of other non-western characters.

下面是一个示例程序来说明该错误:

Here's a sample program to illustrate the error:

using System;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Data.OracleClient;

namespace OracleConnector
{
    class Program
    {        
        static void Main()
        {
            TestString();

            return;
        }

        private static void TestString()
        {
            string connStr = "Data Source = XE; User ID = testuser; Password = secret";

            using (OracleConnection conn = new OracleConnection(connStr))
            {
                conn.Open();
                var cmd = conn.CreateCommand();
                cmd.CommandText = "select 'some text in English language' as a, '储物组合带门/抽屉, 白色 卡维肯, 因维肯 白蜡木贴面' as b from dual";

                var reader = cmd.ExecuteReader();

                reader.Read();
                string sEnglish = reader.GetString(0);
                string sChinese = reader.GetString(1);

                Trace.WriteLine("English from db: " + sEnglish);
                Trace.WriteLine("Chinese from db: " + sChinese);
                Trace.WriteLine("Chinese from the code: 储物组合带门 / 抽屉, 白色 卡维肯, 因维肯 白蜡木贴面");
            }
        }        
    }
}

它输出以下内容:

English from db: some text in English languageဂ
Chinese from db: ¿¿¿¿¿¿/¿¿, ¿¿ ¿¿¿, ¿¿¿ ¿¿¿¿¿e
Chinese from the code: 储物组合带门 / 抽屉, 白色 卡维肯, 因维肯 白蜡木贴面

如您所见,正常代码中的中文字符有效.但不是来自数据库的时候.另外,英文文本中的最后一个字符有些混乱.我还尝试了具有相同结果的相应Mono nuget包.

As you can see, Chinese characters from normal code works. But not when it comes from the database. Also, the last character in the English text is some messed up thing. I've also tried the corresponding Mono nuget package with the same result.

有人知道如何解决此问题吗?

Anyone have any clue how to fix this?

尝试在连接字符串中添加Unicode = True,但是中文字符仍然不起作用.

Tried adding Unicode=True to the connection string but Chinese characters still doesn't work.

推荐答案

请尝试

Environment.SetEnvironmentVariable ("NLS_LANG",".UTF8"); 

在创建connection-Object之前.

System.Data.OracleClient-Implementations使用外部Oracle库,该库假定(至少在Windows上)采用ANSI字符集.

The System.Data.OracleClient-Implementations uses external Oracle libraries, which assumes (at least on Windows) the ANSI-Charset.

设置NLS_LANG-Environmentvariable会通知Oracle-Lib您想要UTF8编码.

Setting the NLS_LANG-Environmentvariable informs the Oracle-Libs that you want the UTF8-Encoding.

(更多)更多详细信息: http://www.oracle.com/technetwork/database/database-technologies/globalization/nls-lang-099431.html

(much) more Details on the NLS_LANG-FAQ-Page: http://www.oracle.com/technetwork/database/database-technologies/globalization/nls-lang-099431.html

这篇关于.NET Core 2 + System.Data.OracleClient.汉字不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆