问题
I have a new install of PostgreSQL (9.6.1) using the Big SQL installer, and a new install of Npgsql (3.2.0) using the .msi installer, but non-ASCII character support seems to be broken.
My connection string is:
<add name="Northwind" connectionString="Database=Northwind;Server=localhost;Port=5432;User Id=postgres;Password=123;Encoding=ASCII;" providerName="Npgsql"/>
I get exception messages like this, when reading back non-ASCII character data:
System.Text.DecoderFallbackException : Unable to translate bytes [C3] at index 22 from specified code page to Unicode.
I have tried using Encoding=UNICODE;
instead, but neither works.
回答1:
You probably have an old Npgsql connection string. Encoding=ASCII
or Encoding=UNICODE
will no longer work when using current (3.2.0) Npgsql to access current (9.6.1) PostgreSQL.
Solution: just remove Encoding=xxx
completely and everything works!
You don't need to 'fix' your Postgres encodings and locales, you don't need to try to find a working Encoding
value to use in the connection string; Ngpsql 3 just works with no Encoding
setting, against the default Postgres install settings on Windows (Encoding=UTF8 | Collate=English_United Kingdom.1252 | Ctype=English_United Kingdom.1252
) (for UK, but I believe the same thing applies for English_United States.1252
for US, and other locales).
(See: https://github.com/npgsql/npgsql/issues/1396)
来源:https://stackoverflow.com/questions/42118301/encoding-ascii-and-encoding-unicode-do-not-work-in-an-npgsql-3-connection-stri