First ASP.NET application against Postgresql


Step 1: We need a driver

There is a free ADO.NET driver for Postgresql

It's available for .NET 1.1 or .NET 2.0. Just download and unzip - you will now get some DLL files (Momo.Security and Npgsql) to reference:

Now - the secret is the connection string. Otherwise we just use code pretty much like we do against MySQL and MSSQL:

 



        public DataTable GetData()
        {
 

            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();

            return dt;


        }
        protected void Page_Load(object sender, EventArgs e)
        {
            GridView1.DataSource = GetData();
            GridView1.DataBind();
        }

And that's it! Here's the result when running:

All in all: I am so impressed. From my novice perspective (which of course is the only angle I can look at Postgresql right now) it's fantastic how easy it was to get it all up and running.

Hope you also give it a try!