How to Connect PostgreSql and VB through ODBC

In this tutorial we assume that PostgreSql database is installed in a separate Linux Serve and throu

In this tutorial we assume that PostgreSql database is installed in a separate Linux Serve and through LAN we will connect this database using ODBC driver and access it via Visual Basic Programme.

Prerequisite: Familiar with Linux, Windows Platform and VB programming knowledge.

Step 1.
First you must have PostgreSql ODBC driver for win32 platform. You can easily download it from PostgreSql official website.

Step 2.
Start the PostgreSql server which is installed in the Linux machine.

Step 3.
Test your LAN connectivity by ping command.
We assume that our PostgreSql Database is on Linux machine and VB programme is on a Windows XP machine and both machines are connected by a Local Area Network. Now from the windows machine ,go to the Start Manu—- > Run — > and type the following command ping –t
Here we give the IP address of the Linux Machine. If you get the replay , then your LAN is OK, otherwise check your LAN and other Network Related matter. Network trouble shoot is out of scope here.
Step 4.
In the Linux Machine where PostgreSql Database is installed, create one database using PostgreSql commands or by using pgAdmin III application.

Step 5.
Now in the Windows machine install the PostgreSql driver. After successful installation ,go to the Control Panel– > Administrative Tools— > Data Sources (ODBC)
Click on it, ODBC Data Source Admin pop up window will appear, on the RHS you will find one ADD button, click on it. Now select the PostgreSql driver from it and press the Finish button. Now system will ask you Data Source Name , Database Name , Database Password etc. Fill up it properly.
DATA SOURCE ………. // Give any Name ,say example
DATABASE NAME …………… //Give the PostgreSql database name
SERVER …………….// Give the IP address of your Linux machine(DB server) ,say
USER NAME ……………// Give the database User name
PASSWORD……………..//Password of the database
PORT ……………… // port no 5432

Save the Form. Your database setting is complete.

Step 6.
Now open your VB project. You have to select ADODB library in VB. For this go to Project … > References … > and select Microsoft ActiveX Data Object 2.1 Library.

Step 7.
Go to the RHS project window and open one Module by click on it.On the module write down the below mention line.
Public conn as New ADODB.Connection
// here conn is the connection variable, you can give any name.
Save it

Step 8.
Open MDI form and on form load section write down this code. “abc”,”postgres”,”password1234”
//here 1st field is Data Source Name , this name should be same as ODBC driver setup Data Source Name.
2nd field is Data Base Name
3rd is Database Password.
These fields should be match with the proper data, otherwise it will not work.

Leave a Reply

Your email address will not be published. Required fields are marked *

  ×  8  =  32