C# + Postgresql = strange character

Asked

Viewed 178 times

1

Greetings Galera

I am working with C# Windows Forms and have already done some tests with postgresql using version 9.1 and everything seemingly normal.

But I came across a bench in version 8.4, using Encoding = "SQL_ASCII".

This way, when I consult the accented characters or with cedilla all come strange.

How to solve this ?

Thank you!!

inserir a descrição da imagem aqui

  • Characters are OK in the bank?

  • If you use UTF-8 it would solve?

  • Then Paulo, using Pgadmin, in the bank is appearing correct, with accents and cedilhas. It seems that it is between C# and postgresql. I’m using Npgsql to run CRUD

1 answer

0

The best way is to use the ISO-8859-1 charset Charset ISO brings the characters you need and is more compatible with UFT8

  • So Hardcorder...the problem is on how to bring it in the right way...where the database shows it correctly...but no c# no..!!

  • I even found this AKI: http://stackoverflow.com/questions/16342198/how-do-i-set-encoding-in-an-npgsqlconnection BUT I DIDN’T UNDERSTAND HOW TO DO THIS

  • ja sei , you have to query the database server by sending this SET Names UTF8 OU SET Names ISO-8859-1

  • this link you sent me also serves, just run set client_encoding = 'WIN1252' in a Postgresql query as if it were a normal command

  • let me get this straight... ?

  • Yes should be soon the first query after connected with the database!

  • if it worked goal as right answer ;)

  • I executed the command Npgsqlcommand cmd = new Npgsqlcommand(); cmd. Connection = cn; cmd.Commandtype = Commandtype.Text; cmd.Commandtext = "set client_encoding = 'SQL_ASCII'"; cn.Open(); cmd.Executenonquery(); AND NOTHING... UTF8, WIN1252, BACK WITH WEIRD CHARACTER THE SAME WAY

  • Try running with this: Encoding iso = Encoding.Getencoding("ISO-8859-1"); Encoding utf8 = Encoding.UTF8; byte[] utfBytes = utf8.Getbytes(Message); byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes); string msg = iso.Getstring(isoBytes); The "Message" variable being the data taken from the Database

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.