Re: PostgreSQL and C#

From: "Daniel Morgan" <danmorg(at)sc(dot)rr(dot)com>
To: "'Matthew Stanfield'" <matthew(at)propertyknowledge(dot)com>
Cc: "'PostgreSQL General Mailing List'" <pgsql-general(at)postgresql(dot)org>
Subject: Re: PostgreSQL and C#
Date: 2002-04-10 13:04:55
Message-ID: 002701c1e090$4f8b5b90$64a43942@danpc
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgadmin-hackers pgsql-general

Thanks for the help. However, I'm more interested in a direct approach
from C# to the PostgreSQL client library. Also, this must be able to
work under Windows, Linux, etc...

I discovered that pq.dll is the PostgreSQL client library under Windows
while libpq.so is library under Linux. This was the cause of my
confusion because I couldn't find libpq.so under Windows. Here is
snippet I used to pinvoke into pq.dll on cgwin. Now, I can help work on
creating a provider for System.Data in Mono C#. Work has already
started on System.Data classes by various people, including Rodrigo
Moya, the maintainer of Gnome-DB.

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[DllImport("pq.dll")]
public static extern IntPtr PQconnectdb(String
conninfo);
// PGconn *PQconnectdb(const char *conninfo)

[DllImport("pq.dll")]
public static extern void PQfinish(IntPtr conn);
// void PQfinish(PGconn *conn)

[DllImport("pq.dll")]
public static extern IntPtr PQexec(IntPtr conn,
String query);
// PGresult *PQexec(PGconn *conn, const char
*query);

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
IntPtr dbconn;
String sConnInfo;
String sQuery;

sConnInfo = "host=localhost dbname=test
user=danmorg password=viewsonic";
sQuery = "insert into sometable " +
"(tid,tdesc) " +
"values('dan','morgan') ";

dbconn = PQconnectdb(sConnInfo);

if(dbconn == IntPtr.Zero)
Console.WriteLine("dbconn is null");
else
{
Console.WriteLine("assuming successful
connection");
PQexec(dbconn,sQuery);
PQfinish(dbconn);
}
}
}
}

-----Original Message-----
From: Matthew Stanfield [mailto:matthew(at)propertyknowledge(dot)com]
Sent: Tuesday, April 09, 2002 1:39 PM
To: Daniel Morgan
Cc: PostgreSQL General Mailing List
Subject: Re: [GENERAL] PostgreSQL and C#

This email just got returned to me as a failed delivery so I am
resending
it, apologies if anyone gets it twice. Matthew

> I really want to use C# and .NET to query PostgreSQL 7.2 databases?

Hi Daniel,

The way I have done it is by using ODBC. You will need to download:
odbc.net from the microsoft site (URL below) and the postgresql odbc
driver
(URL below).

Some of the .net CLR database orientated classes are specific to
Microsoft's SQL Server so you can't use them. odbc.net provides a suite
of
classes such as OdbcConnection, OdbcCommand and OdbcDataReader; which,
so
far, have been really easy to use (see mini example below).

> Do I need to get the source the PostgreSQL database itself?

Absolutely not! I highly recommend you using odbc.net to connect to
postgresql, it is simple to use (and learn), fast and has, so far, been
bug
free.

postgresql odbc site:
http://odbc.postgresql.org

odbc.net is here:
http://msdn.microsoft.com/downloads/default.asp?URL=/downloads/sample.as
p?url=/MSDN-FILES/027/001/668/msdncompositedoc.xml

Mini Example:

string query = "SELECT * FROM testtable";
OdbcConnection odbcCon = new OdbcConnection("DSN=PostgreSQL");
OdbcCommand odbcCom = new OdbcCommand(query, odbcCon);
odbcCon.Open();
OdbcDataReader odbcReader =
odbcCom.ExecuteReader(CommandBehavior.CloseConnection);
Then you can start 'reading'.

Good luck,

..matthew

In response to

Responses

Browse pgadmin-hackers by date

  From Date Subject
Next Message Medi Montaseri 2002-04-11 03:26:46 Re: PostgreSQL and C#
Previous Message Matthew Stanfield 2002-04-09 17:38:33 Re: PostgreSQL and C#

Browse pgsql-general by date

  From Date Subject
Next Message Daniel Ferreira Felix 2002-04-10 13:51:37 ENC: Datatype time PostGreSql 7.2.1
Previous Message Paulo Jan 2002-04-10 11:14:42 Re: Problems building 7.2.1 RPMs