Recovering badly formatted string when it contains accented words

Asked

Viewed 144 times

1

In my application I am having trouble fetching the data in the Postgresql database, the words with accentuations when retrieved from the database and dealing in DataReader are coming badly formatted as in the image below.

  • In the database with correct format

SS

  • Being read in the DataReader

SS

The Encoding of the bank is in UTF8 and the Character type is in Portuguese_brazil.1252 if it helps.

I’m reading the data as follows:

var command = new NpgsqlCommand("Select * from Empresa", con);
con.Open();
NpgsqlDataReader dr = command.ExecuteReader();
while (dr.Read())
{
     Empresa e = new Empresa();
     e.IdEmpresa = Convert.ToInt32(dr["id_empresa"]);
     e.NomeFantasia = dr["nome_fantasia"].ToString();
}

Follow the image to demonstrate the language better than this database of mine:

inserir a descrição da imagem aqui

  • 1

    Show how you are reading the data. I don’t quite understand if you are using UTF8 or CP1252, they are different things. Maybe the problem is there.

  • right, I put + details answering my question to put the image

  • You must [Dit] the question and not answer it with an addition.

  • How is the connection? It configures the charset? Have you tried using UTF8Encoding.UTF8.GetString() or something like that?

  • Pãs is clearly UTF-8 being displayed in 1252. Either you convert to 1252 to display, or use an output that shows UTF-8.

  • Another example: Action in UTF-8 is Açà in win1252 (or even ISO-8859-1).

  • how can I set the charset or convert it to 1252 to show the correct accents? I’m new to C#.

Show 2 more comments

1 answer

2


Solved, in my Connectionstring I reported a different encoding as follows.

  • Before:

    Server=127.0.0.1;User Id=postgres; Password=123;Database=gemax;
    
  • Afterward:

    Server=127.0.0.1;User Id=postgres; Password=123;Database=gemax; Encoding=UNICODE;
    

According to the Mono-Project documentation, if there is a problem with accents in UTF-8, recommend using UNICODE.

  • You can accept the answer there on the left under the vote. See [tour].

Browser other questions tagged

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