C#通过ODBC连接SQL Anywhere时变音符号(öäü)显示异常求助
It looks like you're hitting a classic character encoding mismatch issue here. The problem is that your ODBC connection is interpreting the database's character data using the wrong codepage—specifically, the byte representing 'ö' (0xF6 in CP1252/ISO-8859-1) is being decoded as CP437, where that byte maps to '÷'. That's exactly why you're seeing "G÷ppel" instead of "Göpel".
Here are the steps to fix this:
1. Use the correct charset in your connection string
First, find out what charset your database is actually using. Run this query in SQL Center:
SELECT DB_PROPERTY('CharSet')
Take the result (like cp1252, iso-8859-1, etc.) and add it to your connection string using the CHARSET parameter. For example, if the charset is cp1252:
var connectionString = "Driver={SQL Anywhere 17};Host=localhost;Server=myserver;db=mydatabase;uid=dba;pwd=sql;trusted_connection=yes;CHARSET=cp1252;";
This tells the ODBC driver to decode the database's bytes using the correct charset, so you'll get the right characters directly without extra conversion.
2. Manual conversion if the connection string fix doesn't work
If for some reason the CHARSET parameter isn't being respected (sometimes ODBC drivers have quirks), you can reverse the incorrect encoding in C#:
var name = dataRow.Get<string>("NAME"); // Convert the incorrectly decoded string back to raw bytes using CP437, then re-decode with the correct charset byte[] rawBytes = Encoding.GetEncoding(437).GetBytes(name); string fixedName = Encoding.GetEncoding(1252).GetString(rawBytes); // Replace 1252 with your database's actual charset Console.WriteLine(fixedName); // Should now show "Göpel"
Since your output shows '÷' instead of 'ö', we know the wrong encoding is CP437 here—this should map the bytes back correctly.
3. Verify with SQL Center first
To confirm this is the issue, update your SQL Center connection to include the correct CHARSET parameter. If the umlauts start displaying properly there, the same change will fix your C# code.
The customer's program works because it's already using the correct charset in its connection string—matching that should resolve your problem.
内容的提问来源于stack exchange,提问作者Olli

