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.
- Install MySQL v5.0
- Install Visual Studio (use the express editions if you don’t have full versions)
- 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.
- Start building a database
- A nice application to use is ModelRight 3 community edition
- Create two tables and add some fields and join one of the tables to the other in a parent-child relationship
- Generate (engineer) the database to MySQL
- Open Visual Studio
- Go to the Server Explorer, right-click on Data Connections
- Click Add Connection
- Change the datasource to MySQL Database and make sure the Data provider is .NET Framerwork Data Provider for MySQL and click OK
- Login to the server with the following:
- Server name: localhost
- User name: root
- Password: the initial password you gave when setting up MySQL
- 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.
- Create a website application using Visual Studio (File | New | Website)
- Pick ASP.NET Web Site (or ASP.NET AJAX-Enabled Web Site) if you have the .NET AJAX Toolkit installed.
- Add the connection string to Web.Config
- Update App_Code data layer classes to use the schema of the database
- Add an Object Data Connection to the web page with the following settings
- Put a DataGridView on the screen and connect it to the Object Data Connection object with these settings
- Run the application