0
votes

I cant get Npgsql connection string to work. I've tried some ways, for example

NpgsqlConnection conn = new NpgsqlConnection("Server=192.168.1.220;User Id=pgsql;Password=pgsql;Database=pgsql;");

or

var conn = new NpgsqlConnection("Host=192.168.1.220;Username=pgsql;Password=pgsql;Database=pgsql");

both throws me exception:

The type initializer for 'Npgsql.NpgsqlConnectionStringBuilder' threw an exception.

Im running Windows XP 64bit with VS 2010. I've already tried to connect from that machine to my pgsql server using ODBC or pgAdmin III - both works ok.

Could be this problem related to my Npgsql installation? I was not able to install it with NuGet, because I got following error:

PM> Install-Package Npgsql Installing 'Npgsql 3.2.1'. Successfully installed 'Npgsql 3.2.1'. Adding 'Npgsql 3.2.1' to Project. Uninstalling 'Npgsql 3.2.1'. Successfully uninstalled 'Npgsql 3.2.1'. Install failed. Rolling back... Install-Package : Could not install package 'Npgsql 3.2.1'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.0,Profile=Client', but the packag e does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author. At line:1 char:16 + Install-Package <<<< Npgsql + CategoryInfo : NotSpecified: (:) [Install-Package], InvalidOperationException + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand

I've tried also to switch target framework to 4.0 or 3.5, but result was exactly the same. So I installed Npgsql simply by extracting .dll from MSI installation, then load it as reference.

Does anyone know how to make connection string to work?

1
From your nuget installation exception, it would seem like you're trying to install Npgsql into a .NET Framework 4.0 project - this isn't supported. The lowest supported version is 4.5. - Shay Rojansky
If you still get the exception in a .NET 4.5 (or later) application, please post the full exception you're getting, include the full stack trace and any inner exceptions. It's not possible to know what's going on with just "The type initializer threw an exception". - Shay Rojansky
It seems that problem has been exactly as you descibed, but I need .NET 4.0 to make it run on WinXP, so, I solved my problem by switching from Npgsql to Devart basic pgsql library. Thank you. - geosys

1 Answers

0
votes

You forgot about the port. Maybe this is the cause. The standard PostgreSQL server listening on port 5432.

NpgsqlConnection conn = new NpgsqlConnection("Server=192.168.1.220;Port=5432;User Id=pgsql;Password=pgsql;Database=pgsql;");

Example Connection String

Maybe you haven't a library Mono.Security.dll. It is necessary in the older version less then 3.0 or is enabled the system firewall?