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

Ankit Jain radical at gmail.com
Mon Jan 3 12:48:46 EST 2005


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)
                        // there is a libgda PostgreSQL provider
                        string connectionString =
                                "Provider=PostgreSQL;" +
                                "Addr=;" +
                                "Database=test;" +
                                "User ID=postgres;" +

                        IDbConnection dbcon;
                        dbcon = new OleDbConnection(connectionString);

                        //IDbCommand dbcmd = dbcon.CreateCommand();

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

                        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 = null;
                        dbcmd = null;
                        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
in <0x00048> (wrapper remoting-invoke-with-check)
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? 


More information about the Mono-devel-list mailing list