Connect to a MySQL Server


This tutorial will show you how to get a basic MySQL Connection going in C# Express 2008.

You will first want to get the MySQL Connector from their website at This Link

Select a mirror close to you then download a Windows Binary ZIP File. Unpack the zip file and run the installer.
NOTE: If you have an earlier version of 5.2 you MUST uninstall it in order to install this new one.

After it is installed you can start Visual C# and create a new Windows Forms project.

TutorialNinja Image

After the new project is created you will want to go to your solution explorer and right click on the project name and click "Add Reference"

TutorialNinja Image

After you do that go to the Browse tab and open
Code
plaintext


Click the MySql.Data.dll and hit enter thus adding it to your new project.

After it has been added you can double click your form and where it has

Code
plaintext


Add After
Code
plaintext


Lets not have the program connect directly... Instead, lets place in our user details :D

Create 3 labels,
3 textboxes
and 1 button.

Here is the information as follows.

Label 1
Code
plaintext


Label 2
Code
plaintext


Label 3
Code
plaintext


Textbox 1
Code
plaintext


Textbox 2
Code
plaintext


Textbox 3
Code
plaintext


Button 1
Code
plaintext


This here is my outcome ;)
TutorialNinja Image

Your form design is done! Now onto the raw code.

Double click your Test Connection button and under

Code
plaintext


Hit enter one more time and place the following lines of code.
Code
plaintext


TutorialNinja Image

Go back into your void of pressing the button and add the following:
Code
plaintext


After that you can press F5 too see it in action. Fill out the fields and press "Test Connection" and you should get something like the following:

TutorialNinja Image

And that concludes this tutorial! Stick around and my next one will add onto this one and allow you to connect to a database and select things from tables ;)
ShadowMage's Avatar
Author:
Views:
8,002
Rating:
Posted on Monday 19th January 2009 at 12:11 AM
aaron1988
aaron1988's Avatar
Nice tutorial