[Mono-list] sample MySQL code on web site doesn't work.

Lefty myleftfoot@spro.net
Wed, 22 Jan 2003 00:54:23 -0800


Hi all, i was trying to get the sample code on the mono web site to work 
and I found that there was errors in the code.
http://www.go-mono.com/mysql.html

I'm not an expert on C# nor on MySQL but i need to make a few changes to 
the code in order to get it to compile and run.

one the page there is two samples, one that uses Mono.Data.MySql and the 
other ByteFX.Data.MySQLClient

I'm using
SuSE 8.1
Mono 0.19
MySQL 3.23.52

I couldn't get Mono.Data.MySql to work. I keept getting dll import error 
with libmySQL.But the same changes to the code are needed.

on my sample I needed to open the connection and
change the line
dbcmd.ConnectionString = sql;
to
dbcmd.CommandText = sql;

For the ByteFX sample same thing plus rename
MySqlConnection to MySQLConnection


Here's my sample version of it. Hope it helps.

----
  using System;
  using System.Data;
  using ByteFX.Data.MySQLClient;

  public class Test
  {
     public static void Main(string[] args)
     {
        string connectionString =
           "Server=localhost;" +
           "Database=test;" +
           "User ID=tester;" +
           "Password=mypwd;";
        IDbConnection 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 firstname, lastname FROM employee";
        dbcmd.CommandText = sql;
        IDataReader reader = dbcmd.ExecuteReader();
        while(reader.Read()) {
             string FirstName = reader["firstname"].ToString();
             string LastName = reader["lastname"].ToString();
             Console.WriteLine("Name: " +  FirstName + " " + LastName);
        }
        // clean up
        reader.Close();
        reader = null;
        dbcmd.Dispose();
        dbcmd = null;
        dbcon.Close();
        dbcon = null;
     }
  }
----

Regards,
     -- Lefty