I run PostgreSQL on a FreeBSD server. Why, because I'm a cheap lazy hack with no money to invest in M$ and SQL Server license. I only started programming with VB 2010 Express a few weeks ago. And trying to find information on how to get started connecting to a PostgreSQL server through .NET.

This may be something trivial but for newbies like me I thought I'd share this code snippet. It's nothing fancy but it will make a connection to the database and insert some new data. You can build on it from there.

First, you will need a file called Npgsql.dll. There are several locations for getting this, just do a search for it and you'll find one of them. Open a new project in VB.NET and add a reference to it. Click on the browse button and point to the location where you have this .dll file. Once it's added you can copy this code into your project. Make the necessary changes to the server name, and database name.

Code:
Imports Npgsql

Public Class Form1

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        Dim myConnection As NpgsqlConnection = New NpgsqlConnection()
        Dim myCommand As NpgsqlCommand
        Dim mySQLString As String

        myConnection.ConnectionString = "Server=10.0.0.254;Port=5432;Database=vbnet01;User Id=postgres;Password=postgres;"
        myConnection.Open()
        mySQLString = "INSERT INTO DRAWINGS (id, QuoteNum, DWGNo, DrawnBy) VALUES (18, 213317, 77692, 'FLG');"

        myCommand = New NpgsqlCommand(mySQLString, myConnection)
        myCommand.ExecuteNonQuery()

        myConnection.Close()
    End Sub
End Class