问题
I have a postgresql database that was created with "SQL_ASCII" and template "template0". It holds data in Portuguese so I have data such as "Não", "Feijão", "Avô". When I try to retrieve rows that have these kind of characters "~^" it does not work, otherwise it works.
Errors:
- "Error parsing column 6 (sempresa=0 - String)"}
- Message = "Can not convert byte [C3] in index 0 of the code page specified for Unicode."
I'm using Npgsql and Dapper
<packages>
<package id="Dapper" version="1.50.2" targetFramework="net45" />
<package id="Npgsql" version="3.1.7" targetFramework="net45" />
</packages>
I searched a lot but nothing works. I've tried:
Setting up client encoding in connection string:
var sqlBuilder = new NpgsqlConnectionStringBuilder
{
Host = host,
Database = database,
Username = user,
Password = password,
Pooling = false,
ClientEncoding = "SQL_ASCII"
// I also tried "UNICODE", "utf8", "win-1252"
};
I also tried to run a non-query command to change enconding before selecting
// I tried a lot of encondings: SQL_ASCII, win-1252, unicode
connection.Execute("set client_encoding = 'SQL_ASCII'");
var data = connection.Query<T>(strSQL);
I CANNOT change database encoding or recreate database sinse the application will run against lots of production databases.
I hope nobody flags this question as duplicated sinse I really tried to solve with other questions but nothing I found worked.
回答1:
I've added support for non-UTF8 encodings to Npgsql. This should really be used only in special edge cases such as this, where the database was created with SQL_ASCII, contains non-ASCII characters, and an encoding change is impossible.
I've backported this support to the next patch version, 3.1.8 (seems harmless enough). There's some trouble with the build server at the moment, but if you wait a few days you should have a CI package of 3.1.8 available with the new support at http://myget.org/gallery/npgsql. Follow https://github.com/npgsql/npgsql/issues/392 for more updates/progress.
来源:https://stackoverflow.com/questions/39145146/cannot-select-some-rows-with-npgsql-on-a-portuguese-database