Thursday, August 6, 2009

PostgreSQL and .NET

Recently my .NET project required me to use PostgreSql as the database server.
The steps to connect to PostgreSQL from .NET are as follows
  1. Download the odbc driver from http://www.postgresql.org/ftp/odbc/versions/msi/
  2. Run the installer

Connection string:

Dim hostname As String = "localhost"
Dim port As String = "5432"
Dim user As String = "myuser"
Dim pass As String = "mypass"
Dim db As String = "myDB"
Dim connstr As String = Nothing


connstr = "DRIVER={PostgreSQL ANSI};SERVER=" + hostname + ";UID=" + myuser + ";PWD=" + mypass + ";DATABASE=" + db

Dim DB_connection As Odbc.OdbcConnection = Nothing

DB_Connection = New OdbcConnection(connstr)

'Open the connection
DB_Connection.Open()


Enjoy~

Mehdi