This weekend I started working on FICSiface, a sort of toolkit to getting software hooked up to the Free Internet Chess Server (FICS). My initial plan was to have a class that could connect to FICS, send data, and raise events when FICS sends data to it. This came together quite quickly (remind me to talk about asynchronous IO soon), and of course I got carried away. I decided it would be nice to use it to hook up Blikskottel , my chess engine, which would run as a Windows service. All of this glue stuff is being written in C#, because one day it would be nice to run it on Mono. Then I got carried away some more. While I was testing on FICS (my computer account, Skottel, having been reactivated by an extremely helpful and industrious FICS admin - thanks!), Blik (another computer, Rookie by Marcel Kervinck) came on and made some random joke. So I thought, in light of the fact that Blikskottel is now based in Scotland, it would be cool if Skottel could make self-deprecating jokes about Scotsmen from time to time. In order not to be boring, it would need a biggish database of jokes - and Google came through handsomely.
Now where to put them?
Being a fan of MySQL (chiefly on Linux), I decided it would be neat to try it on Windows for this application. The MySQL 5 Installer downloaded and ran without a hitch, as did the GUI tools (and very nicely done, too - well done MySQL). Then I installed MySQL Connector/NET, a provider for MySQL which acts just like System.Data.SqlClient. Believe it or not, it all just worked - consider the following code snippet:
using (MySqlConnection conn = new MySqlConnection("server=localhost; database=fics; uid=foo; pwd=bar"))
// get a joke we haven't used for a while
MySqlCommand cmd = null;
cmd = conn.CreateCommand();
cmd.CommandText = "select ID, Joke from jokes order by LastUsed LIMIT 1";
cmd.CommandType = CommandType.Text;
MySqlDataReader rdr = cmd.ExecuteReader();
jokeID = rdr.GetInt32(0);
joke = rdr.GetString(1);
Simple as that. Of course, MySQL 5 can actually do stored procs as well, so we'll have to investigate that next, but to cut a long story short, it was surprisingly easy and familiar to use MySQL from a .NET application.
Why Would Anyone Bother?
I know what you're going to say - why would you not use SQL Server? Or SQL Server Express, that's free? Of course, "free" is important to me - I don't own a SQL Server license, and I won't countenance using software illegally. However, SQL Server Express has limitations - just the one CPU, just the 1GB of RAM, just the 4GB of database size, just the one operating system. What happens if I collect more than 4GB of Scotsman jokes ("Did ye hear the one aboot the sasannach whae tol' 4GB of Scots jokes?" "Och, nae, laddie." "Seems a caber fell on him and he had nae backup!")? More importantly, what if I want to run it on Linux/BSD/whatever? MySQL solves all these problems, and is quite a nice database to my way of thinking. But most importantly, it's Free. I have the source code, and if MySQL engages in any unpleasant business practices aimed at reducing the utility of the customer, there would be a fully-supported fork in seconds. If I have issues with the database which the vendor isn't interested, I can fix them - although I have no issues, and come to think of it, not a lot of time to be grovelling around in the code of one of the world's eminent databases - but it's the thought that counts!