Read Chinese Character from SQL(SQL Server 2005) Database Table Column and Display Chinese Character

asp.net , csharp Hong Kong SAR
  • 13 years ago
    Hi!

    I have a table like this below and it doesn't only contain English Names but it also contain Chinese Name.
    CREATE TABLE Names
    (FirstName NVARCHAR (50),
    LastName NVARCHAR (50));
    I tried to view the column using SQL Query Analyzer, It didn't display Chinese Character.
    I know that SQL Server 2005 is using UCS-2 Encoding and Chinese Character uses Double Byte Character Set (DBCS) Encoding.
    I want to read the FirstName and LastName columns and display in Window Form Data Grid and ASP.NET Grid View.
    I tried to use this code below and it didn't work. It convert some of the English Name to Chinese Character and it display the chinese character and some still in the original unreadable characters.
    Does anybody know how to read those character from SQL Table and display the correct Chinese Character without converting the English Name into Chinese also?
    Thanks

    int codePage = 950;
    StringBuilder message = new StringBuilder();
    Encoding targetEncoding = Encoding.GetEncoding(codePage);
    byte[] encodedChars= targetEncoding.GetBytes(str);
    .
    message.AppendLine("Byte representation of '" + str + "' in Code Page '" + codePage + "':");
    for (int i = 0; i < encodedChars.Length; i++)
    {
    message.Append("Byte " + i + ": " + encodedChars[i]);
    }

    message.AppendLine(" RESULT : " + System.Text.Encoding.Unicode.GetString(encodedChars));
    Console.Writeline(message.ToString());

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The greatest performance improvement of all is when a system goes from not-working to working.” - John Ousterhout