Retrieve data from PostgreSQL Database using ADO.Net “System.Data.Odbc” (VB.Net)
NickName:Asfand Iqbal Ask DateTime:2013-04-15T23:15:08

Retrieve data from PostgreSQL Database using ADO.Net “System.Data.Odbc” (VB.Net)

I have been asked to do some research on PostgreSQL and after running the following piece of code system returns two entries (Unnamed Portal 1 and Unnamed Portal 2) when we read DBDataReader object, however I would like to read all the recods from the these two portals and then load my business object with the information as each portal has more than 5 records in tables.

After exploring various pages on the internet I came across that there is a Third Party library ("NpgSQL" > NpgsqlDataReader ) offers this functionality and it is free. I have plugged their library into my solution and it works as per expections. However, I don't want to use this third party software and just wondering if there is a way I can read records from these two portals then it would be great.

Please note that you can give us a solution in c# as I am familiar with that. Thanks.

Code Snippet that I am currently using to retrieve data

Dim oConnection As DbConnection = DbProviderFactories.GetFactory(CONST_PROVIDER_ODBC).CreateConnection
    oConnection.ConnectionString = "Valid connection string goes here"
    oConnection.Open()

    ' Start a transaction as it is required to work with cursors in PostgreSQL
    Dim tran As DbTransaction = oConnection.BeginTransaction

    ' Define a command to call stored procedure show_cities_multiple
    Dim command As DbCommand = DbProviderFactories.GetFactory("System.Data.Odbc").CreateCommand
    command.CommandText = "SELECT MyTestFunction()"
    command.CommandType = CommandType.StoredProcedure
    command.Connection = oConnection
    command.Transaction = tran

    ' Execute the stored procedure and obtain the first result set
    Dim dr As DbDataReader = command.ExecuteReader()

    ' Output the rows of the first result set
    While dr.Read()

    'This is where system show Unnamed Portal 1 or Unnamed portal 2 at the second time.
        MsgBox(String.Format("{0}", dr(0)))
    End While

    tran.Commit()

-- Function to retrieve data.

CREATE OR REPLACE FUNCTION MyTestFunction() RETURNS SETOF refcursor AS $$
DECLARE
  ref1 refcursor;           -- Declare cursor variables
  ref2 refcursor;                             
BEGIN
  OPEN ref1 FOR SELECT * FROM MyTable1;
  RETURN NEXT ref1;                                                                              -- Return the cursor to the caller

  OPEN ref2 FOR SELECT * FROM MyTable2;
  RETURN NEXT ref2;                                                                              -- Return the cursor to the caller
END;
$$ LANGUAGE plpgsql;

Environment I am currently working on is following:-

32 Bit Machine.
Visual Studio 2010 + SP1
ODBC Prodiver: PostgreSQL Unicode 9.01.02.00
ADO.Net (System.Data.Odbc)

Certainly, I am looking forward to hear any feedback and your help would be much appreciated.

Copyright Notice:Content Author:「Asfand Iqbal」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/16018791/retrieve-data-from-postgresql-database-using-ado-net-system-data-odbc-vb-net

More about “Retrieve data from PostgreSQL Database using ADO.Net “System.Data.Odbc” (VB.Net)” related questions

Connecting ado.net Entity data model to postgresql database

I'm new to C#, ado.net and visual studio 2012, just to make that clear from the beginning. I'm trying to connect an ADO.NET Entity Data Model to a postgresql database. I tell VS to 'generate mdoel...

Show Detail

Retrieve data from PostgreSQL Database using ADO.Net "System.Data.Odbc" (VB.Net)

Though I have been using SQL Server, Oracle from last decade, I have been asked to do some research on PostgreSQL and after some initial investigation it is evident that I am now stuck on retrieving

Show Detail

Connect ADO.NET Entity Data Model to Postgresql database

I'm creating an ASP MVC 3 website. I have a Postgresql database - I'm using Entity Framework 6 - I have referenced Npgsql and Npgsql.EntityFramework and installed dotConnect for PostgreSQL. The

Show Detail

Fastest way to retrieve data from a database in .NET?

Using ADO.NET, what is the fastest way to retrieve data from the database and populate the data into my business objects? Which one should I use? DBDataReader , DBDataAdapter, or any other classes...

Show Detail

MultipleActiveResultSets for postgresql and ado.net entity data model

Im using visual studio, postgresql database and ado.net entity data model. In the connectionstring, Im unable set MultipleActiveResultSets=True. Usually when I connect to sql server with

Show Detail

how to use postgresql with ado.net entity framework in asp.net mvc 2

In my current project, we are using visual studio 2008, sql server 2008, mvc 2 with ado.net entity framework (ado.net entity data model), linq and developing a web application. This application wor...

Show Detail

Postgres Database not available in ADO.NET Entity Data Model

I am trying to create entities from a postgres database using the Visual Studio ADO.NET Entity Data Model, however Postgres Database is not listed as one of the data sources. I have installed dotc...

Show Detail

Retrieve data from PostgreSQL Database using ADO.Net “System.Data.Odbc” (VB.Net)

I have been asked to do some research on PostgreSQL and after running the following piece of code system returns two entries (Unnamed Portal 1 and Unnamed Portal 2) when we read DBDataReader obje...

Show Detail

How to retrieve data using EF Database First

I have a console application. I'm trying to use EF Database First. I used ADO.NET Entity Data Model, then EF Designer from Database. Then I connected to the database, and classes are generated. Now...

Show Detail

Retrieve data from postgresql DB without django models

I have googled tutorials/examples on how to retrieve data from a postgresql database which already has tables, but most of the them are using Models. As in they are tutorials about making a DB from

Show Detail