I have a bunch of Cyrillic-like text in a MSSQL database and need to convert it to Cyrillic in C#.
So... Ðàáîòà â ãåðìàíèè
should become
Работа в германии
Any suggestions?
I should add that the closest I've gotten is ?aaioa a aa?iaiee
Here's the code I'm using:
str = Encoding.UTF8.GetString(Encoding.GetEncoding("Windows-1251").GetBytes(drCurrent["myfield"].ToString()));
str = Encoding.GetEncoding(1251).GetString(Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding(1251), Encoding.UTF8.GetBytes(str)));
ADO.Net exposes all string types from SQL Server provider as C# strings, which implies that they were already converted to Unicode. For non-unicode source columns (as yours obviously is) like
char(n)
orvarchar(n)
, the ADO.Net SQL Server provider uses the source collation information to determine the encoding. Therefore if your non-unicode SQL Server data gets represented in .Net with the wrong encoding, it must had been presented to the provider with the wrong collation. Choose an appropriate collation for your data and ADO.Net provider for SQL Server will translate it using the appropriate encoding. For example, as documented in Collation and Code Page Architecture, Cyrillic collations will result in code page 1251, which is very likely what you want. The articles linked contain all the information you need to fix your problem.The result is: