Click here to Skip to main content
11,931,104 members (62,715 online)
Click here to Skip to main content
Add your own
alternative version


1 bookmarked

Connect WPF with Wamp Server MySql

, 5 Aug 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
The easy way to connect to Mysql using Wamp server on WPF


We always need to use database in our project, in WPF we usually use SQLITE, but what if we want to deploy our database? Then you can now work with WampServer and it is so easy to do that.

Using the Code

First of all, you create a new database in the WampServer using PhpMyAdmin, then create tables.

To connect with this database, you must add the Reference MySqlData, you can find it when you download this sample.

For the local connection, the server must be localhost, if you don't set a password, then it is Empty by default and enter the name of your database that you created.

Add this following code:

MySql.Data.MySqlClient.MySqlConnection conn;
private void ConnectDatabase()
    string cn = "server=localhost; user id=root; password=''; database=cabinet";
    conn = new MySql.Data.MySqlClient.MySqlConnection(cn);

After successful Connection, you can now retrieve or add some information on the database.

Here is an example of how to read data:

string mySelectQuery = "SELECT * FROM Patient";
MySqlCommand filmsCommand = new MySqlCommand(mySelectQuery, conn);

MySqlDataReader reader = filmsCommand.ExecuteReader();

while (reader.Read())
      int  Numero = reader.GetInt16("Num");
       string name = reader.GetString("Nom");


PS: You must always close the SqlCommand after any Transaction.

If you now want to update your data, then insert the following code:

string mySelectQuery = "INSERT INTO Patient ( Nom , Prenom , Adresse , Telephone ,
Date_Naissance , Date_Ouverte) VALUES ('Bouhlel', 'BHL', 'Sfax','22108069','29/08/1991','" +
DateTime.Now.Date.ToShortDateString() + "')";

MySqlCommand filmsCommand = new MySqlCommand(mySelectQuery, conn);



Connecting to database has become the easiest thing that we can do now, though we still face problems when it comes to executing the transactions.


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


About the Author

Anis Derbel
Software Developer (Junior) Microsoft Student Partners
Tunisia Tunisia
I study Software Engineering , 23 years old , I'm motivated with all Technologies of Microsoft.
Since I have been in the Community of Microsoft as Microsoft Student Partners, I developped many apps on the platform Windows and Phone. Now , it's time to share what I learn here and I'am ready to help Everyone.
You can contact me at any time (
Group type: Organisation

9 members

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 5 Aug 2014
Article Copyright 2014 by Anis Derbel
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid