[Mono-devel-list] MySQL + C#

Colt D. Majkrzak cmajkrzak at charter.net
Sat Jun 19 16:42:03 EDT 2004


Hi, I'm fairly new to c# and the mono project so forgive me if this question
is rather trivial.

 

I'm trying to port some of my Linux programs into c#, well 90% of them
require MySQL to function.  I found the example code on the mono site for
making a client connection, but I'm receiving the following error on
execution (happens on Linux (RH9) or my XP machine w/ Mono installed)

 

C:\>mono test.exe

 

Unhandled Exception: System.InvalidCastException: Cannot cast from source
type t

o destination type.

in <0x00124> Test:Main ()

 

[spider at serv1 test]$ mono test.exe

 

Unhandled Exception: System.InvalidCastException: Cannot cast from source
type to destination type.

in <0x0013a> Test:Main ()

 

-----Code---

using System;

 using System.Data;

 using ByteFX.Data.MySqlClient;

 

 public class Test

 {

    public static void Main()

    {

       string connectionString =

          "Server=localhost;" +

          "Database=database;" +

          "User ID=username;" +

          "Password=password;";

       IDbConnection dbcon;

       dbcon = new MySqlConnection(connectionString);

       dbcon.Open();

       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 pkg_name, pkg_baseprice " +

           "FROM packages";

       dbcmd.CommandText = sql;

       IDataReader reader = dbcmd.ExecuteReader();

       while(reader.Read()) {

            string PkgName = (string) reader["pkg_name"];

            string Price = (string) reader["pkg_baseprice"];

            Console.WriteLine("Name: " +

                  PkgName + " " + Price);

       }

       // clean up

       reader.Close();

       reader = null;

       dbcmd.Dispose();

       dbcmd = null;

       dbcon.Close();

       dbcon = null;

    }

 }

 

 

Thank in advance.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20040619/b0c11212/attachment.html 


More information about the Mono-devel-list mailing list