C# : Unable to print data as unicode characters from MYSQL database

杀马特。学长 韩版系。学妹 提交于 2019-12-10 09:57:27

问题


Hi I have developed a windows application which allows user to save data and see data in tamil font. I installed 'Bamini' font (Tamil font), and set textboxs and datagridview to Bamini font. I am able to save and retrieve data in tamil.

The problem is the tamil data i enter is encoded and saved in database for example: if i enter 'இந்தியா' in textbox and save, it is saved as ",e;j_ah" in mysql db (i have set the column character set as utf8). Due to this when i get the data and try to print it, it is printing ",e;j_ah" instead of 'இந்தியா'.

Can anyone let me know what i am doing wrong here?!

Code that i am using to insert the string:

textBox1 values is 'இந்தியா' (since textbox font is set to 'Bamini' tamil font)

     string insertdata = "INSERT INTO tamil (country) VALUES (@cnt)";
     MySqlCommand cmd = new MySqlCommand(insertdata,connection);
     connection.Open();
     cmd.Parameters.AddWithValue("@cnt",textBox1.Text);
     cmd.ExecuteNonQuery();
     connection.Close();

Database affected as follows:

      tablename: Tamil
      Sno   Country
      1     ,e;j_ah

Table Structure:

          CREATE TABLE `tamil` (                              
            `sno` int(11) auto_increment NOT NULL,                        
            `Description` varchar(50) NOT NULL,                          
            `Country` varchar(50) character set utf8 NOT NULL,                         
            KEY `id_sno` (`sno)                             
          ) ENGINE=InnoDB DEFAULT CHARSET=latin1 ; 

回答1:


Can anyone let me know what i am doing wrong here?!

You are using a visually-encoded font.

In this scheme, you press the comma key on the keyboard, and type a regular character U+002C COMMA ,. The text field is set in a font where the shape of a comma make it look like a Tamil Letter I, but it's still really a comma.

A comma will be stored in the database, and searching tools will match it as a comma; if you pull it back out of the database and display it in the Bamini font then it will look like a Tamil Letter I, but display it in any standard font, like the one you're using to inspect your database, and it will look like a comma.

Visually-encoded fonts are the way we used to cope with language scripts that didn't have a standard encoding, but they should not be used today—chuck Bamini in the bin.

Modern operating systems ship a native Tamil keyboard and font (eg under Windows, Nirmala UI). Using this approach, the user would type into a normal text field (that had no special font set) and get a real Unicode character U+0B87 Tamil Letter I , that should look just the same in the database and behave semantically appropriately.




回答2:


After a long list of trials, i finally found an alternative solution to print tamil characters in my printer. Note: Hardware Tech support informed me that many thermal printers wont accept tamil characters that are sent through raw printer helper class.

So i designed a crsytal report and tried printing, which was immediate success. (My printer is 3inch thermal printer)




回答3:


Put something like this in the connection string:

id=my_user;password=my_password;database=some_db123;charset=utf8;

And change Description to CHARACTER SET utf8 (or utf8mb4).

See this for more debugging: http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored




回答4:


Something goes wrong with the UTF-8 encoding of the string. ",e;j_ah" for sure isn't the UTF-8 representation of your string. I recommend bypassing the UTF-8 feature of the DB altogether and use a simple BLOB type for your "Country" column, which stores a plain byte array of variable length. Then use the UTF-8 codec of .NET and encode/decode yourself, storing the encoded byte array in the BLOB column.

So change the declaration of "Country" to:

`Country` BLOB NOT NULL,   

Use Encoding.UTF8.GetBytes() and Encoding.UTF8.GetString() to encode/decode your Tamil strings.




回答5:


Basically, Bamini is not unicode standard. It has it own encodings so whenever you read you need to decode it which means you need to set bamini font on the contents. when you try to print the system doesn't set to bamini font.

so solution should be either use unicode fonts instead of bamini or set bamini font while printing.



来源:https://stackoverflow.com/questions/45529498/c-sharp-unable-to-print-data-as-unicode-characters-from-mysql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!