.NET and MySQL

Searching for a document that helps to connect a .NET web application with MySQL is hard to find. So, I thought I would provide instructions I put together while developing my own applications.

The .NET infrastructure is very flexible and is easier to use than people think. Here are the steps that I went through to get everything working and connected. Because I program in C# this tutorial is presented in that language. This is an outline of the process and steps to accomplish this tutorial. It helps to keep the ideas in check and that there are no missing subjects.

  1. Install MySQL v5.0
  2. Install Visual Studio (use the express editions if you don’t have full versions)
  3. Install the MySQL 5.0 .NET Data Connector v5.2 (for example mysql-connector-net-5.2.1.zip) or other connector of your programming choice.
  4. Start building a database
    1. A nice application to use is ModelRight 3 community edition
    2. Create two tables and add some fields and join one of the tables to the other in a parent-child relationship
    3. Generate (engineer) the database to MySQL
  5. Open Visual Studio
  6. Go to the Server Explorer, right-click on Data Connections
  7. Click Add Connection
  8. Change the datasource to MySQL Database and make sure the Data provider is .NET Framerwork Data Provider for MySQL and click OK
  9. Login to the server with the following:
    1. Server name: localhost
    2. User name: root
    3. Password: the initial password you gave when setting up MySQL
    4. Database name: This is the name of the database you will attach to. It’s possible you haven’t set one up yet, see the Building a MySQL Database below.
  10. Create a website application using Visual Studio (File | New | Website)
  11. Pick ASP.NET Web Site (or ASP.NET AJAX-Enabled Web Site) if you have the .NET AJAX Toolkit installed.
  12. Add the connection string to Web.Config
  13. Update App_Code data layer classes to use the schema of the database
  14. Add an Object Data Connection to the web page with the following settings
  15. Put a DataGridView on the screen and connect it to the Object Data Connection object with these settings
  16. Run the application

Leave a Reply