Click here to Skip to main content
13,258,763 members (47,835 online)
Click here to Skip to main content
Add your own
alternative version


1 bookmarked
Posted 5 Aug 2014

Connect WPF with Wamp Server MySql

, 5 Aug 2014
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 <a href="">MySqlData</a>, 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
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Organisation

9 members

You may also be interested in...


Comments and Discussions

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