Adding PostgreSQL db to Asp.Net web project on Windows 7

>> Wednesday, January 18, 2012

Last night I was trying to connect an existing Asp MVC 3 app to use an PostgreSQL database.  It is using nHibernate, so I'm still going to have to determine if I need to tweak anything there, but for now just wanted to connect properly.

After installing Postgre, I needed to install the ODBC connector in order to establish a connection in my .Net application.  There was a little trick, when I used the ODBC configurator from the Start menu, I didn't see the newly installed ODBC adapter!  WTF?  Turns out, since I was running a 64 bit version of my OS, I had to run c:\windows\syswow64\odbcad32.exe in order to set my new ODBC driver.  There is also another way...

Install NpgSQL adapter, I used the latest .Net 4.0 install.  After referencing the Npg dll in your project (plus an associated Mono dll), you can simply use their wrappers in order to connect, here you can get the gist:

using NpgSQL;
var connstring = String.Format("Server=localhost;Port=5432;User Id=;Password=xxx; Database=DbName;");
var conn = new NpgsqlConnection(connstring);
const string sql = "SELECT LastName FROM Users ORDER BY LastName LIMIT 10";
var da = new NpgsqlDataAdapter(sql, conn);
var ds = new DataSet();


ADmin April 20, 2013 at 8:22 AM  

Online stores offer various profits to business people on the grounds that they don't need to possess the features they pitch

  © Blogger templates Sunset by 2008

Back to TOP