Your first application


How to connect to Postgresql using .NET

This sample will show you the basics on how to connect to Postgresql. The first thing we need to do is set a reference to the driver dll(s).

When you downloaded the drivers and unzipped them you might have seen there are two dll files.

The Mono.Security and Npgsql. My guess is that Mono.Security implements MD5 password crypting, but I am not sure and really don't care either :)

The trouble is basically just to specify the connection string. Connection strings are decribed here in the article on Postgresql connection strings.



            Npgsql.NpgsqlConnection oConn = new Npgsql.NpgsqlConnection("Server=192.168.10.4;Port=5432;Userid=postgres;Password=stefan;Protocol=3;SSL=false;Pooling=true;MinPoolSize=1;MaxPoolSize=20;Encoding=UNICODE;Timeout=15;SslMode=Disable;Database=test");
            oConn.Open();

            DataSet oDataSet = new System.Data.DataSet("tab1");
            Npgsql.NpgsqlDataAdapter oAdapter = new Npgsql.NpgsqlDataAdapter("select * from testtable", oConn); 
            oAdapter.Fill(oDataSet, "tab1"); 
            DataTable dt = oDataSet.Tables["tab1"]; 
            oConn.Close();

Cause the syntax is just the same as when working against SQL Server or MySQL. It's just the prefix in front of Connection/Command/DataAdapter etc  that needs some change.