Stephen Hill's Bloggie Writing about my interests; Programming and Formula One.

Using MySQL with ASP.Net

One year ago

For the first six months of 2009 I have started a European Union funded project to create a website for local livestock farmers to publish information relevant to them. It's quite a simple website with a few interactive features and there will be a database behind the scenes storing all the information on the site.

My programming language of choice is ASP.Net C# and naturally I would choose Microsoft SQL Server as the backend database, which we had to buy. When I got a quote for a single CPU license, I was amazed at the price... just over £5,000! Obviously I can't afford that for my own projects and SQL Express has some limitations I cannot live with.

I needed an inexpensive or free alternative solution for my new project which was flexible enough for our short and long term needs. In short, we decided to use MySQL and although there are "better" open source databases, MySQL is more than capable and is well supported by the community.

I've never used MySQL with ASP.Net, only with the LAMP stack, and so I had to research how to access the database from my code. Conveniently, the MySQL community have written an ADO.Net driver for the .Net framework. Simple download and add the DLL to your project and a new MySql.Data.MySqlClient namespace will become available.

The good news is they have kept all the same method and property names, all that has changed is the class names, making converting your existing code easier. So lets get down to some examples... Here's how to execute a select statement using the built-in SqlClient class:

String sSQL = "Select Top 1 * From Posts "; sSQL += "Where PO_Date > GetDate()-7 "; SqlCommand cSQL = new SqlCommand(sSQL); SqlConnection cConnection = _ new SqlConnection("server=local;database=Blog;uid=X;password=X"); cSQL.Connection = cConnection; SqlDataAdapter oDataAdapter = new SqlDataAdapter(cSQL); DataTable oDataTable = new DataTable(); oDataAdapter.Fill(oDataTable);

And here's how to do the same statement using the MySqlClient namespace.
I have highlighted the code that has changed.

String sSQL = "Select * From Posts "; sSQL += "Where PO_Date > Now()-7 "; sSQL += "Limit 1; "; MySqlCommand cSQL = new MySqlCommand(sSQL); MySqlConnection cConnection = _ new MySqlConnection("server=local;database=Blog;uid=X;password=X"); cSQL.Connection = cConnection; MySqlDataAdapter oDataAdapter = new MySqlDataAdapter(cSQL); DataTable oDataTable = new DataTable(); oDataAdapter.Fill(oDataTable);

As you can see, the class names have changed and some parts of the SQL statement have changed also to take into account some of the differences between the databases. To be honest, the differences between the database languages will take you the most time to learn. The changes in class names can be sorted with a quick find and replace.

So go away and get MySQL installed on a Linux or Windows box and start enjoying an open source database... It really is that simple and to help you in other areas not covered in this post, here are some useful resources.

Previous Comments

  1. Name Mike Griffiths Monday 26 January 2009 1:48 PM

    As a developer you can get a very low cost copy of SQL Server from Microsoft. This is intended for development only and can't be used for production tasks . Just a thought for next time...

  2. Name Stephen Hill Monday 26 January 2009 2:49 PM

    Hi Mike,

    Thanks for the comment, could you please give me details on this low cost SQL Server licence?

    My new project is a commercial venture and so I would have to purchase an SQL licence per physical CPU and at over £5,000 per licence, it isn't a viable solution.

    If the licences had been around a few hundred pounds per physical machine, I might had considered sticking with SQL Server, but their too pricey for my small start-up, so I've had to use MySQL instead.

    Many Thanks
    Stephen

  3. Name Robert Hanlon Tuesday 27 January 2009 7:21 PM

    One thing that I've noticed with MySQL and ADO.NET: unsigned ints do not come through the MySQL provider correctly. I had an unsigned int32 field in the schema, but had problems if it was over the 2^31 limit. Turns out that if I loaded it into a signed int64, it worked correctly. Did not play well with uints.

    Is anybody else familiar with this oddness? I assumed it was a provider malfunction, but I could be mistaken. Could it be that I'm just doing something wrong?

  4. Name Stephen Hill Wednesday 28 January 2009 9:50 AM

    Hi Robert,

    Your quite right, you do need a Int64 in order to store an unsigned int from MySQL.

    This is because MySQLs unsigned int max value is greater than .net signed Int32 max value.

    I'd be happy to hear more details if this isn't the case :)

    Many Thanks
    Stephen

  5. Name trendbender Wednesday 28 January 2009 10:57 AM

    Entity framework can work with MySQL

  6. Name Ivan Dimitrov Thursday 05 February 2009 10:02 AM

    Hello and from me,
    You can obtain MS SQL Server 2008 Web Edition for 25$ per month by signing Microsoft SPLA license. This program is for service providers, so when your product must be online you have to sign up for this program. You are not allowed to use any copy of Windows Server for hosting if you are not SPLA partner.

    Regards,
    Ivan
    MCAD.NET

  7. Name Ian Tebbutt Thursday 05 February 2009 10:13 PM

    I've been in the same situation, but use NHibernate, which has abstracted the DB layer away entirely. Not always 100% cleanly to move between environments, but not to shabby at all. Have you thought about that approach. BTW I'm interested in what your bovine software does from a proffesional perspective, could you email me details?

    Mr Telly

  8. Name How to play the game of craps Monday 29 June 2009 11:07 AM

    I would have one question though.What should I use to create the model of a MySQL database? I mean, I can’t use ADO.NET Entity Data Model like you did, sine I can’t see MySQL tables, so am I doomed? Is there any other way to hand-code? What file should I choose when I click Add New Item on <a href="http://www.crapsgameplay.net/">How to play the game of craps</a> the Models directory?If only I could get a tip on where to start, I’d be grateful.
    thanks again

Leave a Comment