[Mono-list] Problems with postgres.

Angel Reyes areyes@agconsultoria.com.mx
Wed, 19 Nov 2003 12:06:05 -0600


--=-s/biwNRjE27Mx15IWcqb
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hola Roberto-

I've to modify the Npgsql example from
http://go-mono.com/postgresql.html , check this code:

================ postgresTest.cs ==============
using System;
using System.Data;
using Npgsql;
 
 public class Test 
 {
    public static void Main(string[] args)
    {
       string connectionString = 
          "Server=127.0.0.1;" +
          "Database=DBF;" +
          "User ID=angel;" +
          "Password=********;";
       //IDbConnection dbcon;
       NpgsqlConnection dbcon = new NpgsqlConnection(connectionString);
       dbcon.Open();
       //dbcon = new NpgsqlConnection(connectionString);
       IDbCommand dbcmd = dbcon.CreateCommand();
       // requires a table to be created named employee
       // with columns firstname and lastname
       // such as,
       //        CREATE TABLE employee (
       //           firstname varchar(32),
       //           lastname varchar(32));
       string sql = 
           "SELECT codemp, cnombre " +
           "FROM empresa";
       dbcmd.CommandText = sql;
       IDataReader reader = dbcmd.ExecuteReader();
       while(reader.Read()) {
            String FirstName = (String)reader["codemp"];
            String LastName = (String)reader["cnombre"];
            Console.WriteLine("Name: " + 
                 FirstName + " " + LastName);
       }
       // clean up
       reader.Close();
       reader = null;
       dbcmd.Dispose();
       dbcmd = null;
       dbcon.Close();
       dbcon = null;
    }
 }

=========================================

Compiling...
mcs -r Npgsql.dll -r System.Data.dll postgresTest.cs

Check Npgsql.dll is located in the same directory as System.Data.dll,
usually /usr/lib.




On Tue, 2003-11-18 at 19:18, Roberto Jimeno wrote:

> Hi there!
> 
> Tim, Rodrigo, Daniel, Rafael or someone willing to
> help a mono/sql newbie here.
> 
> I'm using Mono 0.28 installed from an RPM file (and I
> have reasons to stick to the RPM file rather than
> using CVS)
> 
> I'm writing some C# code which requires to select some
> information from a bunch of already-filled tables on a
> postgres database.
> 
> I tried to use Mono.Data.PostgreSQL unsuccessfully as
> well as Npgsql (more details on this at the bottom of
> the message). Now I don't know what to do: Should I
> keep on trying with either of those data providers?
> Should I try to use ByteFX with postgres? Where can I
> find more information about it?
> 
> 
> Thanks in advance for helping me.
> 
> 
> 
> Details on the tests and failures with both data
> providers:
> 
> >From http://go-mono.com/postgresql.html I copied
> verbatim the examples for Mono.Data.PostgreSqlClient
> and Npgsql named them
> TestExample.Mono.Data.PostgreSqlClient.cs and
> TestExample.Npgsql.cs respectively, and then followed
> the instructions found there, in order to compile and
> use the examples:
> 
> When I tried to compile the one using PostgreSqlClient
> , it failed with "Cannot find type `PgConnection'"
> 
> Afterwards, I tried to compile the one using Npgsql,
> which also failed, this time indicating "Use of
> unassigned local variable `dbcon'" on line 15.
> 
> >From somewhere (I can't remember where, but the source
> file is also attached) I copied another test example
> which compiles on my system, although when I attempt
> to run the .exe file using mono, it complains about an
> unhandled exception ("A null value was found where an
> object instance was required")
> 
> 
> The command lines Im using to build (or attempt to
> build) the test examples are as follows:
> 
> $mcs TestExample.Mono.Data.PostgreSqlClient.cs -r
> System.Data.dll -r Mono.Data.PostgreSqlClient.dll
> 
> $mcs TestExample.Npgsql.cs -r System.Data.dll -r
> Npgsql.dll
> 
> $mcs OtherTestExample.cs -r System.Data.dll -r
> Mono.Data.PostgreSqlClient.dll
> 
> 
> =====
> Roberto Jimeno
> 
> __________________________________
> Do you Yahoo!?
> Protect your identity with Yahoo! Mail AddressGuard
> http://antispam.yahoo.com/whatsnewfree

-- 
Angel
http://bukox.tripod.com

--=-s/biwNRjE27Mx15IWcqb
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
  <META NAME="GENERATOR" CONTENT="GtkHTML/3.0.9">
</HEAD>
<BODY>
Hola Roberto-<BR>
<BR>
I've to modify the Npgsql example from <A HREF="http://go-mono.com/postgresql.html"><I><U>http://go-mono.com/postgresql.html</U></I></A> , check this code:<BR>
<BR>
================ postgresTest.cs ==============<BR>
using System;<BR>
using System.Data;<BR>
using Npgsql;<BR>
 <BR>
 public class Test <BR>
 {<BR>
&nbsp;&nbsp;&nbsp; public static void Main(string[] args)<BR>
&nbsp;&nbsp;&nbsp; {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string connectionString = <BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;Server=127.0.0.1;&quot; +<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;Database=DBF;&quot; +<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;User ID=angel;&quot; +<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;Password=********;&quot;;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //IDbConnection dbcon;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NpgsqlConnection dbcon = new NpgsqlConnection(connectionString);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbcon.Open();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //dbcon = new NpgsqlConnection(connectionString);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IDbCommand dbcmd = dbcon.CreateCommand();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // requires a table to be created named employee<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // with columns firstname and lastname<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // such as,<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CREATE TABLE employee (<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; firstname varchar(32),<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lastname varchar(32));<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string sql = <BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;SELECT codemp, cnombre &quot; +<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;FROM empresa&quot;;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbcmd.CommandText = sql;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IDataReader reader = dbcmd.ExecuteReader();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(reader.Read()) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String FirstName = (String)reader[&quot;codemp&quot;];<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String LastName = (String)reader[&quot;cnombre&quot;];<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&quot;Name: &quot; + <BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FirstName + &quot; &quot; + LastName);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // clean up<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reader.Close();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reader = null;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbcmd.Dispose();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbcmd = null;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbcon.Close();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbcon = null;<BR>
&nbsp;&nbsp;&nbsp; }<BR>
 }<BR>
<BR>
=========================================<BR>
<A HREF="mailto:mono-list@lists.ximian.com"></A><BR>
Compiling...<BR>
mcs -r Npgsql.dll -r System.Data.dll postgresTest.cs<BR>
<BR>
Check Npgsql.dll is located in the same directory as System.Data.dll, usually /usr/lib.<BR>
<BR>
<BR>
<BR>
<BR>
On Tue, 2003-11-18 at 19:18, Roberto Jimeno wrote:
<BLOCKQUOTE TYPE=CITE>
<PRE><FONT COLOR="#737373"><I>Hi there!

Tim, Rodrigo, Daniel, Rafael or someone willing to
help a mono/sql newbie here.

I'm using Mono 0.28 installed from an RPM file (and I
have reasons to stick to the RPM file rather than
using CVS)

I'm writing some C# code which requires to select some
information from a bunch of already-filled tables on a
postgres database.

I tried to use Mono.Data.PostgreSQL unsuccessfully as
well as Npgsql (more details on this at the bottom of
the message). Now I don't know what to do: Should I
keep on trying with either of those data providers?
Should I try to use ByteFX with postgres? Where can I
find more information about it?


Thanks in advance for helping me.



Details on the tests and failures with both data
providers:

&gt;From </FONT><A HREF="http://go-mono.com/postgresql.html"><U>http://go-mono.com/postgresql.html</U></A><FONT COLOR="#737373"> I copied
verbatim the examples for Mono.Data.PostgreSqlClient
and Npgsql named them
TestExample.Mono.Data.PostgreSqlClient.cs and
TestExample.Npgsql.cs respectively, and then followed
the instructions found there, in order to compile and
use the examples:

When I tried to compile the one using PostgreSqlClient
, it failed with &quot;Cannot find type `PgConnection'&quot;

Afterwards, I tried to compile the one using Npgsql,
which also failed, this time indicating &quot;Use of
unassigned local variable `dbcon'&quot; on line 15.

&gt;From somewhere (I can't remember where, but the source
file is also attached) I copied another test example
which compiles on my system, although when I attempt
to run the .exe file using mono, it complains about an
unhandled exception (&quot;A null value was found where an
object instance was required&quot;)


The command lines Im using to build (or attempt to
build) the test examples are as follows:

$mcs TestExample.Mono.Data.PostgreSqlClient.cs -r
System.Data.dll -r Mono.Data.PostgreSqlClient.dll

$mcs TestExample.Npgsql.cs -r System.Data.dll -r
Npgsql.dll

$mcs OtherTestExample.cs -r System.Data.dll -r
Mono.Data.PostgreSqlClient.dll


=====
Roberto Jimeno

__________________________________
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard</FONT>
<A HREF="http://antispam.yahoo.com/whatsnewfree"><U>http://antispam.yahoo.com/whatsnewfree</U></I></A></PRE>
</BLOCKQUOTE>
<PRE><TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<PRE>-- 
Angel
http://bukox.tripod.com</PRE>
</TD>
</TR>
</TABLE>
</PRE>
</BODY>
</HTML>

--=-s/biwNRjE27Mx15IWcqb--