Cyrillic coding in C #

I have a bunch of cyrillic text in an MSSQL database and you need to convert it to Cyrillic in C #.

So ... Getting Started

should become

Work in Germany

Any suggestions?

Should I add that the closest I got? aaioa a aa? iaiee

Here is 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))); 
+8
c # sql-server encoding
source share
2 answers
 // To find out source and target const string source = "Ðàáîòà â ãåðìàíèè"; const string destination = "  "; foreach (var sourceEncoding in Encoding.GetEncodings()) { var bytes = sourceEncoding.GetEncoding().GetBytes(source); foreach (var targetEncoding in Encoding.GetEncodings()) { if (targetEncoding.GetEncoding().GetString(bytes) == destination) { Console.WriteLine("Source Encoding: {0} TargetEncoding: {1}",sourceEncoding.CodePage,targetEncoding.CodePage); } } } // Result1: Source Encoding: 1252 TargetEncoding: 1251 // Result2: Source Encoding: 28591 TargetEncoding: 1251 // Result3: Source Encoding: 28605 TargetEncoding: 1251 // The code for you to use var decodedCyrillic = Encoding.GetEncoding(1251).GetString(Encoding.GetEncoding(1252).GetBytes(source)); // Result:    
+16
source share

ADO.Net provides all row types from the SQL Server provider as C # strings, which means that they have already been converted to Unicode. For source columns other than unicode (like yours, obviously), like char(n) or varchar(n) , the ADO.Net SQL Server provider uses the source mapping to determine the encoding. Therefore, if your SQL Server data other than unicode is presented in .Net with the wrong encoding, it must be presented to the provider with the wrong sort. Select the appropriate collation for your data, and the ADO.Net provider for SQL Server will translate it using the appropriate encoding. For example, as described in Collation and Code Page Architecture , cyrillic mappings will result in code page 1251 , which is very likely what you want. Related articles contain all the information you need to resolve your issue.

 using System; using System.Text; using System.Data.SqlClient; using System.Windows.Forms; public class Hello1 { public static void Main() { try { using (SqlConnection conn = new SqlConnection("server=.;integrated security=true")) { conn.Open (); // The .cs file must be saved as Unicode, obviously... // string s = "  "; byte[] b = Encoding.GetEncoding(1251).GetBytes (s); // Create a test table // SqlCommand cmd = new SqlCommand ( @"create table #t ( c1 varchar(100) collate Latin1_General_CI_AS, c2 varchar(100) collate Cyrillic_General_CI_AS)", conn); cmd.ExecuteNonQuery (); // Insert the same value twice, the original Unicode string // encoded as CP1251 // cmd = new SqlCommand ( @"insert into #t (c1, c2) values (@b, @b)", conn); cmd.Parameters.AddWithValue("@b", b); cmd.ExecuteNonQuery (); // Read the value as Latin collation // cmd = new SqlCommand ( @"select c1 from #t", conn); string def = (string) cmd.ExecuteScalar (); // Read the same value as Cyrillic collation // cmd = new SqlCommand ( @"select c2 from #t", conn); string cyr = (string) cmd.ExecuteScalar (); // Cannot use Console.Write since the console is not Unicode // MessageBox.Show(String.Format( @"Original: {0} Default collation: {1} Cyrillic collation: {2}", s, def, cyr)); } } catch(Exception e) { Console.WriteLine (e); } } } 

Result:

 --------------------------- --------------------------- Original:    Default collation: Ðàáîòà â ãåðìàíèè Cyrillic collation:    --------------------------- OK --------------------------- 
+4
source share

All Articles