[Mono-devel-list] Connecting to a db using OleDbConnection

Daniel Morgan danielmorgan at verizon.net
Mon Jan 3 14:08:23 EST 2005


The OleDb provider at System.Data.OleDb probably has bit-rotten.  It has not
kept up with Mono nor libgda.

Possible suggestions:

- get the source to mcs which has the OleDb provider at:
  mcs/class/System.Data/System.Data.OleDb and fix any bugs.  
  Look at how System.Data.OleDb platform invokes into libgda
  via the DllImport attributes.

- use GDA# which is C# bindings to libgda. GDA# is part of GTK#.  
  However, GDA# does not implement 
  any ADO.NET interfaces.  I think if GDA# is to take off, 
  it would need to implement these ADO.NET interfaces 
  like IDbConnection, IDbCommand, IDataRecord, IDataReader, and
DbDataAdapter

- use the ODBC provider at System.Data.Odbc instead

- use a Managed provider instead.
  Such as: Npgsql for PostgreSQL
           ByteFX.Data.MySqlClient for MySQL 
           or MySQL's own data provider: MySql.Data


-----Original Message-----
From: mono-devel-list-admin at lists.ximian.com
[mailto:mono-devel-list-admin at lists.ximian.com] On Behalf Of Ankit Jain
Sent: Monday, January 03, 2005 12:49 PM
To: mono-devel-list at lists.ximian.com
Subject: [Mono-devel-list] Connecting to a db using OleDbConnection


Hi,

I'm trying to compile a simple test program available at
http://www.go-mono.com/oledb.html to connect to a db. I believe
OleDbConnection uses libgda as the backend to make the actual connection to
the database. My installation of libgda is working fine. I've tried adding
entries for a PostgreSQL and a MySQL server in the config file and it works
fine(tested via gda-test, it can connect .. fetch data etc).

But the C# program is not being able to connect. The code:

using System;
using System.Data;
using System.Data.OleDb;

public class Test
{
        public static void Main(string[] args)
        {
                try{
                        // there is a libgda PostgreSQL provider
                        string connectionString =
                                "Provider=PostgreSQL;" +
                                "Addr=127.0.0.1;" +
                                "Database=test;" +
                                "User ID=postgres;" +
                                "Password=";

                        IDbConnection dbcon;
                        dbcon = new OleDbConnection(connectionString);
                        dbcon.Open();

                        //IDbCommand dbcmd = dbcon.CreateCommand();

                        string sql =
                                "SELECT firstname, lastname " +
                                "FROM employee";
                        IDbCommand dbcmd = new OleDbCommand(sql,
(OleDbConnection)dbcon);

                        Console.WriteLine("CmdText= {0}\n",
dbcmd.CommandText);
                        //dbcmd.CommandText = sql;
                        IDataReader reader = dbcmd.ExecuteReader();
                        while(reader.Read()) {
                                string FirstName = (string)
reader["firstname"];
                                string LastName = (string)
reader["lastname"];
                                Console.WriteLine("Name: " +
                                                FirstName + " " + LastName);
                        }
                        // clean up
                        reader.Close();
                        reader = null;
                        dbcmd.Dispose();  
                        dbcmd = null;
                        dbcon.Close();
                        dbcon = null;
                }catch(Exception e){
                        Console.WriteLine("Exception : {0}\n", e.Message);
                }
        }
}

And this is the output:

Unhandled Exception: System.InvalidOperationException: State != Open in
<0x00088> System.Data.OleDb.OleDbCommand:ExecuteReader
(System.Data.CommandBehavior)
in <0x00048> (wrapper remoting-invoke-with-check)
System.Data.OleDb.OleDbCommand:ExecuteReader
(System.Data.CommandBehavior)
in <0x0000f> System.Data.OleDb.OleDbCommand:ExecuteReader ()
in <0x0003b> (wrapper remoting-invoke-with-check)
System.Data.OleDb.OleDbCommand:ExecuteReader () in <0x0000d>
System.Data.OleDb.OleDbCommand:System.Data.IDbCommand.ExecuteReader
()
in [0x00037] (at /home/radical/dev/mono/Test.cs:45) Test:Main (string[])

Basically, its not being able to make the actual connection, but it doesnt
give the error at dbcon.Open(). It throws the exception when I try to
execute a query..

What could be the problem here? What am i missing? 

Regards,
-Anks
_______________________________________________
Mono-devel-list mailing list
Mono-devel-list at lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list




More information about the Mono-devel-list mailing list