Re: PostgreSQL and C# - Mailing list pgsql-general
From | Daniel Morgan |
---|---|
Subject | Re: PostgreSQL and C# |
Date | |
Msg-id | 002701c1e090$4f8b5b90$64a43942@danpc Whole thread Raw |
In response to | Re: PostgreSQL and C# (Matthew Stanfield <matthew@propertyknowledge.com>) |
List | 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@propertyknowledge.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
pgsql-general by date: