How do I set encoding in an NpgsqlConnection - c#

I have a PostgreSQL database, which uses character encoding WIN1252.
When querying the database, some records will produce an error when trying to read the data, because it is trying to convert it to UTF8. This happens on some foreign names containing certain non-Latin characters.
The error is:
ERROR: 22P05: character with byte sequence 0x81 in encoding "WIN1252" has no equivalent in encoding "UTF8"
It happens when I call Read() on the NpgsqlDataReader.
My connection is defined as:
new NpgsqlConnection("Server=127.0.0.1;Port=5432;Database=xyz;User Id=****;Password=****;");
What can I do to read this data using C#?

I've managed to solve the problem. There is no way of setting the property in the connection string or any of the properties of the NpgsqlConnection or NpgsqlCommand.
However, I was able to set the value of client_encoding in a query. So directly after opening the connection I first executed the (non)query:
set client_encoding = 'WIN1252'
After that, any subsequent command on the same connection used the proper encoding and returned the results without complaints.

I tried to change the connection string but i had no luck with that.
The problem got solved by chaning the database settings file and reload it.
So i started pgadmin and executed
SHOW config_file;
which gave me
C:/Program Files/PostgreSQL/14/data/postgresql.conf
in this file i changed the lc_messages from lang_language.1252 to UTF8.
After that i reloaded this config in pg admin by right click on the server name and press "Reload Configuration".
All settings are now set to UTF8 and it just worked fine.
lc_messages = 'UTF8' # locale for system error message
# strings
lc_monetary = 'UTF8' # locale for monetary formatting
lc_numeric = 'UTF8' # locale for number formatting
lc_time = 'UTF8' # locale for time formatting
...

Related

Create database with UTF8 encoding

I'd want to create database with entityframework / npgsql driver with code-first approach but also UTF encoding.
In PGAdmin 3 I'm unable to change Encoding because its field is readonly
This sql is being shown in PGAdmin when I select my database
CREATE DATABASE db
WITH OWNER = dbuser
ENCODING = 'LATIN2' <------------
TABLESPACE = pg_default
LC_COLLATE = 'pl_PL'
LC_CTYPE = 'pl_PL'
CONNECTION LIMIT = -1;
But I want to change it from LATIN2 to UTF8
I manually tried this:
SET CLIENT_ENCODING TO 'UTF8'
and
show client_encoding
returns
client_encoding_text
UTF8
but despite that PGAdmin 3 still shows that this database has an encoding "LATIN2"
and my application is receiving an exception
PostgresException: 22P05: the character 0xe2889e coding "UTF8" does not have an equivalent in "LATIN2"
So, how can I create db with UTF8 via Entityframework with NPSQL driver for postgre using context.Database.EnsureCreated()?

Japanese characters in host name for Chilkat FTP2

I went through Chilkat's forum where i see that forum is closed and I should post related question here on SO.
I am trying to connect to FTP server using FTP2 of chilkat, but it fails to connect to valid ftp server with valid credentials.
Host name does contain Japanese characters "w10jpnツ新".
My code looks like below:
Ftp2 objFtp = new Ftp2();
const string ChilkatFTPKey = ValidChilKatFTPKey;
bool success = objFtp.UnlockComponent(ChilkatFTPKey);
if (!success)
{
return;
}
objFtp.Hostname = _ftpServer;
Log.Write("Host Name Input: " + _ftpServer); //here I see "w10jpnツ新"
Log.Write("Host Name: " + objFtp.Hostname); //here I see "w10jpnツ新"
As you can see even after setting proper host name to objFtp.Hostname , that property is having garbage value and thus connection attempt fails.
How to fix it?
UPDATE
Initially this question was written to Chilkat technicians, but as now as there is no response from them, I m adding general tags to make it in reach of general people in Hope if they may know what can be possible reason behind such problem.
One thing i could think is if in setter of the property Hostname if they convert Japanese characters into something unwanted. But why would they do it.
I see the problem -- Chilkat is assuming that the Hostname is composed of Latin1 (us-ascii + accented European chars). I'll make the fix and I can provide a pre-release. Which version of the .NET Framework do you use, and which version of Visual Studio?
Also.. the objFtp.Hostname property should be set to a domain name or an IP address. The string "w10jpnツ新" is not a valid domain name. A domain name is something like "xyz.com" -- something that a DNS server can resolve to an IP address.

Load data infile parameter error

LOAD data LOCAL INFILE
'C:/ProgramData/MySQL/MySQL Server 5.6/Uploads/amazon1.xml'
INTO TABLE
amazonxmlfeeddata
CHARACTER SET 'utf8'
LINES STARTING BY '<item_data>' TERMINATED BY '</item_data>'
(#tmp)
SET
item_unique_id = ExtractValue(#tmp, '//item_unique_id'),
item_title = ExtractValue(#tmp, '//item_title'),
item_long_desc = ExtractValue(#tmp, '//item_long_desc'),
item_page_url = ExtractValue(#tmp, '//item_page_url');
This is basically my query and I wrote this is in ASP.NET C#.
I want to execute this through C# in MySQL.
But if I execute through C# it gives me error of (#tmp) that it should be declared. And even after declaring it still throws the same error.
It seems you need to add AllowUserVariables=true; to the MySql connection string, to allow user variables (#tmp).
See: https://dev.mysql.com/doc/connector-net/en/connector-net-connection-options.html

Passing file name with spaces to SPOOL command using SQL Plus gives SP2-0768 Illegral SPOOL command error

My sql file contains
`SPOOL &1;
//sql code to execute
SPOOL OFF;`
The sql file is executed using SQL Plus and SQL Plus is being called from C# code using Process.Start... Code snipped
`var m_StartInfo = new ProcessStartInfo();
m_StartInfo.FileName = "SQLPLUS.EXE";
m_StartInfo.CreateNoWindow = true;
m_StartInfo.UseShellExecute = false;
m_StartInfo.Arguments = String.Format("{0}\"{1}\" \"{2}\"", connectionString, sqlfile, sqlLogFileName);
m_Process = Process.Start(m_StartInfo);
Other code.....`
It works fine and the sqlplus log is created fine at sqlLogFileName location. However if the sqlLogFileName has spaces in between (say like "C:\My Application\log.txt"), then the log file is not created, instead gives the error SP2-0768 Illegal SPOOL command on SQL Plus window
Any suggestion how to resolve this? I am using Oracle 11GR2
You just have to surround your file name with double quotes.
Something like:
spool "Test with spaces.txt"
Or in your case with a parameter:
SPOOL "&1"
I think is best not to use spaces though, as #tvCa explained.
Side Note:
What StarPilot is refering to, I believe, is about the redirect a command output to a file, and that is why it didn't work when you tried to use it.
For example in command prompt you would write:
dir > dir.txt
And that saves the output of dir inside dir.txt.
Oracle software is designed to be used with directories not having whitespaces (which is an accepted standard in Linux/Unix, even though technically you can do otherwise). On Windows, things are different, but the Oracle software has the same idea : it does not like whitespaces. So, the fix is clear : spool to a directory without whitespaces. This is advice, anybody is free to take or not.

Error 400 (Bad Request) when get a file which name contains & character in C# ASP.NET MVC3

I have an issue regarding the filename which contains & character.
I have to get an image which name is: Test&TestAgain.jpg. In ASP.NET MVC3 application, in View side, I put
Url.Content( "~/Content/Images/"+ filename );
In Chrome, I see error 400 Bad Request because of filename which contains &.
I think that & is used for query string and browser interprets file name as query string.
Because filename doesn't contain ? then browser throw that error. (A potentially dangerous Request.Path value was detected from the client (&).)
It is a way (workaround) to fix this without replace that character ?
You need to UrlEncode the path:
Url.Content("~/Content/Images/" + HttpServerUtility.UrlEncode(filename));
Edit:
Seems it doesn't work. The only way I can think of right now is to allow resticted characters and enable VerificationCompatibility by setting in the registry:
// 32-bit IIS
HKLM\SOFTWARE\Microsoft\ASP.NET VerificationCompatibility = 1
// 64-bit IIS
HKLM\SOFTWARE\Wow6432Node\Microsoft\ASP.NET VerificationCompatibility = 1

Categories