I created a tutorial before on How To Connect SQL Server Database Using C# and Perform CRUD Operation. Now I will use the same application that we built in that tutorial to connect to MySql database and perform a CRUD operation.
For this tutorial you need to install MySql Connector. You can download it from https://dev.mysql.com/downloads/connector/net/
Create a database in MySql. I named my database as employeeDb and I created the Employees table with columns Id, FirstName and LastName.
Open the SQLCSHARPCRUD project. If you don’t have it, you can download it from my previous tutorial
How To Connect SQL Server Database Using C# and Perform CRUD Operation
In our project we need to add reference for MySql.Data. To do that right click to References then click Manage Nuget Packages.
Then in Nuget click Browse tab search for mysql then install.
Then open app.config and update appSettings. Change the value of provider to MySql.Data.MySqlClient and the connectionString to your MySql database connection string.
<appSettings> <add key="provider" value="MySql.Data.MySqlClient"/> <add key="connectionString" value="Server=localhost;Port=3306;Database=employeeDb;Uid=root;Pwd=;"/> </appSettings>
Then run your application.