.NET Core 2 + System.Data.OracleClient.中文字符不起作用

2021-11-24 00:00:00 oracle asp.net-core .net-core

我将 .NET Core 2 与几周前在此处发布的 System.Data.OracleClient 包一起使用: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-Charset.

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

设置 NLS_LANG-Environmentvariable 通知 Oracle-Libs 您需要 UTF8 编码.

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

(更多)关于 NLS_LANG-FAQ-Page 的详细信息: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

相关文章