Trouble using/displaying special characters from Oracle db in .Net app

前端 未结 2 989
半阙折子戏
半阙折子戏 2020-12-21 06:52

I have a C#.Net application that accesses data from a commercial application backed by an Oracle 10 db. A couple of fields in the commercial app\'s database (declared as va

2条回答
  •  粉色の甜心
    2020-12-21 07:08

    Certain characters in the WE8ISO8859P1 character set have a different binary representation than the same character in UTF8.

    What I suggest are 2 possible ways

    1) Try using Oracle native data providers for .NET (ODP.NET). May be there is a bug/feature in Microsoft's library System.Data.OracleClient that this adapter do not automatically support converting WE8ISO8859P1 to unicode. Here is a link to ODP.NET

    I hope that there will be a support for this encoding in ODP (but to say true I never checked this, it is only a suggestion)

    2) Workaround: in Dataset, you should create a binary field (mapped to the original table field) and a String field (not mapped to the database). When you load data to the dataset, iterate for each row and perfrom convertion from binary array to string.

    Code should be something like this

    Encoding e = Encoding.GetEncoding("iso-8859-1");
    foreach(DataRow row in dataset.Tables["MyTable"])
    {
        if (!row.IsNull("MyByteArrayField"))
            row["MyStringField"] = e.GetString((row["MyByteArrayField"] as byte[]));
    }
    

提交回复
热议问题