MySQL, considered "the most popular open source database in the world", has gone through a long journey from its humble beginnings as a low-cost database server to a server that drives huge Web sites and important business systems. However, if you are an ASP.NET developer, then you will always encounter a dissatisfaction: MySQL was originally designed for applications on the UNIX platform, so support for Windows is secondary.
But now, the Windows version of MySQL has the same features and stability as the UNIX version, and it is considered a viable database server for Windows development. Now let's see how you can connect to a MySQL database within the .NET framework using ODBC.
Installation
Download and install the Windows version of MySQL. Installation is easy - just follow the prompts and you'll be up and running in no time. If you encounter a problem, you can go to the MySQL forum to seek help and solutions.
To connect ASP.NET and MySQL, you need to use ODBC.NET. Generally speaking, the DataProvider of ODBC.NET is part of the standard .NET framework (version 1.1 and above), so it will be automatically installed with the latter.
Once you confirm that ODBC.NET is installed, you will need to download the ODBC driver for MySQL. Again, the MySQL developers are happy to help - they provide these drivers on their Web site. You can read the FAQ document when downloading the file, which will list all the problems you may encounter during the installation of the MySQL ODBC driver on your system.
All done? Now let's start with some code.
Connecting to MySQL with ASP.NET
One of my favorite things to do is read, and when I'm not writing instructional articles like this one, I find a quiet corner to complete my bibliography list. Unfortunately, I'm not a very organized person, so this often makes a mess.
So what does this have to do with the topic we are going to talk about today? Well, that's the beginning of my first example, which is to create a database of books like the ones in List A.
To create this table, use the following SQL query:
CREATE TABLE `books` (
`id` int(5) NOT NULL auto_increment,
`title` varchar(255) NOT NULL default '',
`authors` varchar(255) NOT NULL default '',
`year_of_publication` year(4) NOT NULL default '0000',
`date_added` date NOT NULL default '0000-00-00',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
To execute this query, you must use the command line client software "mysql.exe" in the "bin" folder in the MySQL installation directory. The following are the specific commands:
c:mysqlbin>mysql -u guest -p test
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 13 to server version: 4.0.12-nt
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql> CREATE TABLE `books` (
-> `id` int(5) NOT NULL auto_increment,
-> `title` varchar(255) NOT NULL default '',
-> `authors` varchar(255) NOT NULL default '',
-> `year_of_publication` year(4) NOT NULL default '0000',
-> `date_added` date NOT NULL default '0000-00-00',
-> PRIMARY KEY (`id`)
-> ) TYPE=MyISAM;
Query OK, 0 rows affected (0.02 sec)
Once the "books" table is created, you can start inserting data. List B lists some items. Now, do the same thing with ASP.NET - execute the SELECT query (Listing C) and display the results in the browser. If everything is fine—the MySQL server is running, the MySQL ODBC driver is installed correctly, and the Books table contains data—you should see a page like the one in Figure A.
Figure C show you the error message that will appear when the script attempts to access a non-existent database table (note that exceptions generated by the inner "try-catch" structure will be handled by the outer structure):
above is an introduction to how to use the DataGrid server control of MySQL and ASP.NET. There's a lot more to be said about how you can use these two technologies; so I hope this article and the other reference resources provided above will help you get a feel for it.