.NET Core 2 + System.Data.OracleClient. Chinese ch

2019-02-28 06:12发布

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: 储物组合带门 / 抽屉, 白色 卡维肯, 因维肯 白蜡木贴面");
            }
        }        
    }
}

It outputs this:

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

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?

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

3条回答
地球回转人心会变
2楼-- · 2019-02-28 07:00

Append ";Unicode=True" to connectionstring and add Environment.SetEnvironmentVariable ("NLS_LANG",".UTF8"); before create connection

string conn = "DATA SOURCE=hostname.company.org:1521/servicename.company.org;PASSWORD=XYZ;USER ID=ABC;Unicode=True"
Environment.SetEnvironmentVariable("NLS_LANG", ".UTF8");
using (DbConnection conn = create_connection(app_conn))
{
    //...
}
查看更多
戒情不戒烟
3楼-- · 2019-02-28 07:10

This is a problem with the System.Data.OracleClient DLL. I am having the same problem where 2, 3, or even 4-byte Unicode characters are getting tacked to the end of my strings.

Switching to Mono.Data.OracleClientCore helped slightly, but I still got some odd characters at the end of some strings (Unicode backspace and backslash).

I just tried the following library, and it seems to work for my needs (so far): https://github.com/ericmend/oracleClientCore-2.0

You will need to re-compile for Windows (change to #define OCI_WINDOWS in OciCalls.cs). Will update this answer if I find that it doesn't continue to work.

Still, I think that we'll have to wait for Oracle to release their .NET Core supported solution for any sort of production ready library.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-02-28 07:14

Please try

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

before creation of the connection-Object.

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

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

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

查看更多
登录 后发表回答