Click here to Skip to main content
15,881,248 members
Articles / Database Development / MySQL
Tip/Trick

How to Connect to MySQL Using C#

Rate me:
Please Sign up or sign in to vote.
4.85/5 (23 votes)
17 Jul 2012CPOL1 min read 539.6K   59   19
How to connect to a MySQL database using the MySQL Connector for .NET.

Introduction

This article shows you how to connect to a MySQL database using the MySQL Connector for .NET. I will also show you how you can update MySQL database records using C#.

Prerequisites

  • Install Visual Studio 2008 or Visual Studio 2010
  • Install MySQL database on your local machine
  • MySQL database admin tool that allows you to create a database and run SQL statements. I am using phpMyAdmin which is a web interface.
  • Download and install MySQL Connector.

Getting Started

Run the XAMPP application and it will automatically install Apache server, MySQL database, and FileZilla. After installing check whether these services are running or not. The following XAMPP control panel shows which of those services are currently running.

Image 1

Now the following steps will show how to connect to a MySQL database using C#.

Step 1

Open MySQL Admin page and create a new database.

Image 2

Step 2

After creating the new database, create a new table.

Image 3

Step 3

After creating the new table, open Visual Studio and click on New Project and name the project. It will open the new project, then click on Solution Explorer (F4), right click on “Reference” to add a new reference into the project. Reference those two .dll files to the project (MySql.dll (Win apps), MySql.web.dll (Web apps)).

Image 4

Step 4

Add namespace to the project.

Image 5Image 6

Step 5

Create a MySQL connection string.

Image 7

Step 6

The following code will insert the data into MySQL table.

Image 8

Step 7

The following function will load the data from the table and bind it into a GridView.

Image 9

Step 8

The final result is shown on the window form:

Image 10

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionPls Pin
Member 1411439631-Jan-19 3:26
Member 1411439631-Jan-19 3:26 
Questionty Pin
Member 1279379114-Oct-16 4:43
Member 1279379114-Oct-16 4:43 
QuestionCRUD in C# using MySQL for newbies Pin
Ehtesham Astute15-Feb-16 17:45
Ehtesham Astute15-Feb-16 17:45 
QuestionHow about accessing in other Lan Computers Pin
Rencyrence17-Nov-15 21:25
Rencyrence17-Nov-15 21:25 
Questionhelpful, but ordinary work Pin
Member 117989125-Jul-15 14:00
Member 117989125-Jul-15 14:00 
GeneralMy vote of 4 Pin
Nidhin David8-Mar-15 6:58
Nidhin David8-Mar-15 6:58 
GeneralMy vote of 2 Pin
sam.hill13-Apr-14 16:38
sam.hill13-Apr-14 16:38 
GeneralRe: My vote of 2 Pin
zamkinos18-Jun-15 20:35
zamkinos18-Jun-15 20:35 
Question[My vote of 1] No sample project Pin
msdevtech29-Dec-13 8:56
msdevtech29-Dec-13 8:56 
Questionthanks it was really helpful for me...!!! Pin
Member 1040181323-Nov-13 6:36
Member 1040181323-Nov-13 6:36 
QuestionExcellent! Pin
Dan Kudela12-Mar-13 1:08
Dan Kudela12-Mar-13 1:08 
GeneralMy vote of 5 Pin
Dave Crump27-Oct-12 0:36
Dave Crump27-Oct-12 0:36 
GeneralMy vote of 3 Pin
Rudolf Grauberger18-Sep-12 12:35
Rudolf Grauberger18-Sep-12 12:35 
GeneralMy vote of 5 Pin
figgikim117-Sep-12 3:52
figgikim117-Sep-12 3:52 
SuggestionUse the using directive... Pin
Rudolf Grauberger15-Aug-12 8:05
Rudolf Grauberger15-Aug-12 8:05 
QuestionCould not load file or assembly 'MySql.Data.CF' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) Pin
PramodBNair19-Jul-12 11:22
PramodBNair19-Jul-12 11:22 
I get the following error. Please help.

Could not load file or assembly 'MySql.Data.CF' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

Only difference I have is instead of installing the .Net MySQL Connector I copied to bin folder. and modified web.config
AnswerRe: Could not load file or assembly 'MySql.Data.CF' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) Pin
Ravindra T C19-Jul-12 15:59
professionalRavindra T C19-Jul-12 15:59 
AnswerRe: Could not load file or assembly 'MySql.Data.CF' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) Pin
Ravindra T C23-Jul-12 21:10
professionalRavindra T C23-Jul-12 21:10 
SuggestionWhat would be great also... Pin
Fréderic Cordier17-Jul-12 20:04
Fréderic Cordier17-Jul-12 20:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.