C#/Oracle: Specify Encoding/Character Set of Query

2019-02-27 07:40发布

I'm trying to fetch some Data of a Oracle 10 Database.
Some cells are containing german umlauts (äöü).
In my Administration-Tool (TOAD) I can see them very well: "Mantel für Damen" (Jacket for Women)
This is my C# Code (simplified):

var oracleCommand = new OracleCommand(sqlGetArticles, databaseConnection);
var articleResult = oracleCommand.ExecuteReader();
string temp = articleResult.Read()["SomeField"].ToString();
Console.WriteLine(temp);

The output is: "Mantel f?r Damen"
Tryed on Debugging (moving mouse over variable), Debug-Window, Console-Window, File.

I think I have to specify the Encoding/Character Set somwhere. But where?

3条回答
淡お忘
2楼-- · 2019-02-27 08:25

.Net CLR strings are [internally] UTF-16 encoded. ADO.Net, at least with SQL Server, handles the translation between the native string format in the database and the UTF-16 encoding used in the .Net CLR.

I suspect that that's true with Oracle's ADO.Net provider as well.

However, Console.WriteLine() is doing its own thing. You can get (or set) the input encoding via Console.InputEncoding and get/set the output encoding via Console.OutputEncoding.

On my machine, Console.WriteLine() displays accented characters properly. The default output encoding on my machine is System.Text.SBCSCodePageEncoding. It's using the IBM 437 aka Windows 1252 code page. And it's using the default raster font 'Terminal'.

If the font your are using doesn't support (at least) the C0 Control and Basic Latin and C1 Controls and Latin-1 Supplement (ISO 8859-1), you're unlikely to have success with accented characters. the IBM 437/Windows 1252 code page is mostly ISO 8859-1, except that code points 0x80 to 0x9F (the C1 control characters) have been assigned glyphs.

查看更多
Summer. ? 凉城
3楼-- · 2019-02-27 08:32

It was a Problem with my OracleConnection:

var oracleConnection = new OracleConnection(connectionString);
oracleConnection.Open();
return oracleConnection;

This fixed it:

oracleConnection.Unicode = true;

(before opening the connection)

By the way: I'm using the DevArt's ADO.NET Provider for Oracle

查看更多
We Are One
4楼-- · 2019-02-27 08:37

you can also add Unicode=true in your connection string

查看更多
登录 后发表回答