[Mono-docs-list] http://www.mono-project.com/MySQL update?

Erik Nordström Andersen erik at erik-n-andersen.dk
Tue Oct 11 18:22:00 EDT 2005


Hi

I hope this is the right place to suggest updates to the homepage.

I was playing with Mono and MySQL and needed an example to get going.
http://www.mono-project.com/MySQL provides an example using the ByteFX
library but states above that the ByteFX library is deprecated. Below I
suggest a new example using the MySQL connector (borrowed from MySQL.com):

using System;
using System.Data;
using MySql.Data.MySqlClient;

class MySqlExample
{
	public static void Main(string[] args)
	{
        string myConnectionString = "Database=test;Data
Source=localhost;User Id=user;Password=passwd";
	    string mySelectQuery = "SELECT id, name FROM customer";
	    MySqlConnection myConnection = new MySqlConnection(myConnectionString);
	    MySqlCommand myCommand = new MySqlCommand(mySelectQuery,myConnection);
	    myConnection.Open();
	    MySqlDataReader myReader;
	    myReader = myCommand.ExecuteReader();
	    // Always call Read before accessing data.
	    while (myReader.Read()) {
	       Console.WriteLine(myReader.GetInt32(0) + ", " +
myReader.GetString(1));
	    }
	    // always call Close when done reading.
	    myReader.Close();
	    // Close the connection when done with it.
	    myConnection.Close();
	}
}



The compile command should be:

mcs MySqlExample.cs -r:System.Data.dll -r:MySql.Data.dll

And running the program:

mono MySqlExample.exe


Let me know if I should provide this in another format, e.g. with markup
or as a patch to a source file I can access somewhere.

Kind regards,

Erik Andersen


More information about the Mono-docs-list mailing list