Click here to Skip to main content
Click here to Skip to main content

How to Connect to MySQL Using C#

, 17 Jul 2012
Rate this:
Please Sign up or sign in to vote.
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.

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.

Step 2

After creating the new database, create a new table.

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)).

Step 4

Add namespace to the project.

Step 5

Create a MySQL connection string.

Step 6

The following code will insert the data into MySQL table.

Step 7

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

Step 8

The final result is shown on the window form:

License

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

About the Author

Ravindra T C
Software Developer L&T IES Mysore
India India
I'm a software developer. I spent most of the time in learning new technologies. I've a keen interest in client-side technologies especially JavaScript and Jquery it is the most beautiful language ever seen.

I like sharing my knowledge and written some non-popular articles. I believe in quality and standards but blames myself for lagging them.

I believe in small things and they makes me happy!

Comments and Discussions

 
GeneralMy vote of 2 Pinmembersam.hill13-Apr-14 16:38 
Question[My vote of 1] No sample project Pinmembermsdevtech29-Dec-13 8:56 
Questionthanks it was really helpful for me...!!! PinmemberMember 1040181323-Nov-13 6:36 
QuestionExcellent! PinmemberDan Kudela12-Mar-13 1:08 
GeneralMy vote of 5 PinmemberDave Crump27-Oct-12 0:36 
GeneralMy vote of 3 PinmemberRudolf Grauberger18-Sep-12 12:35 
GeneralMy vote of 5 Pinmemberfiggikim117-Sep-12 3:52 
SuggestionUse the using directive... [modified] PinmemberRudolf 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) PinmemberPramodBNair19-Jul-12 11:22 
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) PinmemberRavindra T C19-Jul-12 15:59 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 17 Jul 2012
Article Copyright 2012 by Ravindra T C
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid