[Mono-devel-list] Connecting to a db using OleDbConnection
Rafael Teixeira
monoman at gmail.com
Mon Jan 3 13:56:19 EST 2005
I'm not sure what may be causing the error, but I sure recommend to
use the specific providers (NPgSQL for PostgreSQL...) that are more
exercised, because they have better performance and deal better with
the database peculiarities.
If you are looking after OLEBD for easing multidatabase support, I
think some other approach like Canuto's Advanced Data Provider
(http://sourceforge.net/projects/advanced-ado/) is better suited, and
more portable.
Hope it helps,
On Mon, 3 Jan 2005 23:18:46 +0530, Ankit Jain <radical at gmail.com> 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
>
--
Rafael "Monoman" Teixeira
---------------------------------------
I'm trying to become a "Rosh Gadol" before my own eyes.
See http://www.joelonsoftware.com/items/2004/12/06.html for enlightment.
More information about the Mono-devel-list
mailing list