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

Jonel Rienton jonel at road14.com
Mon Jan 3 20:23:25 EST 2005


If I'm not mistaken, to be able to use the reader, the connection has 
to be explicitly opened and explicitly close when you're done.

http://jonel.road14.com
--
I not know English well, but I know 7 computer languages.
								----anonymous


On Jan 3, 2005, at 11:48 AM, Ankit Jain wrote:

> 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