How To Connect To MySQL Database Using C# and Perform CRUD Operation

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.

Advertisements

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s