Monday, 5 May 2014

How to Connect C# to Mysql


First of all you should have Mysql Connector, you can download Here. and please install that connector. After you install it we can go to the next step :) Yipiiy yeay !!!


oke, FYI I'm using microsoft  visual studio 2010, it doesn't really metter if you use different version. i think they all just little bit has different. and this tutorial I use WPF Application. now open your Visual studio, make a new project







 Select Visual C# template and Select WPF Application. Please give name as you like, i named it Tutorial_ConnectToMysql and then click button OK.


 and then you'll brought to it MainWindow.xaml there are consist of 4 parts.

1st is Design part.
the 1st part is design page, it consists 2 page. there are interface and source code(XAML). this picture bellow is the part for interface, so you don't have to type any code. you only need to click and drag all component that you need from toolbox to this interface. if you want move your component you just click and drag till you get enough, you don't have to think the margin size. and if you want to add same properties you only click your component and then click tab property usually on your right project side.
for you who doesn't really like code it's the simple way for you. and i like it :D


and this picture bellow is the design page which use source code. it is really different from interface page. in this side you need type all your component and if you want to add one or more properties you need type it too. for you who doesn't really like code. this way isn't recommended for you. but it's challenging !! so i combine these way :D





2nd error List part. here the part that show your syntax error. after you make a User Interface if you made some mistake, this page would show you where the error is.


3rd Solution Explorer part. here the pert that show all your list project.

4th toolbox, toolbox used for your user interface design. you just click and drag to your interface page. on this side all your tool is provide.


okay, it's all the page on your project work. now lets begin.
1. From Toolbox click and drag button to Interface design page. like this



if you look at source code page for design (XAML) it'll automatically added button source code.

<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="182,112,0,0" Name="button1" VerticalAlignment="Top" Width="75" />

Content is mean the tittle showed on button. i change it to Test !

2. Next step is please double click on your button. you'll brought to MainWindow.xaml.cs. here that we place our syntax.


in private void button1_Click(object sender, RoutedEventArgs e) we'll type all code.
before you type your source code for connection. you need to add your Mysql Connection newly installed
 :) so, click tab Solution Explorer and right click on Reference, click add Reference



It'll open new window. select tab .NET and select MySql.Data and then click button OK.
Automatically in Reference MySql.Data added.

okay, now time to coding !!! 
add this above your namespace

type this inside private void button1_Click() like this picture bellow
private void button1_Click(object sender, Reroute dEventArgs e)
{
}

nb: 
string db = "Payroll" --> it's my database name, you should adjusted to your database
string pass = "" --> it's my dbms password, if you have pass you should adjusted to your dbms pass

i have 2 ways to declare connection to mysql, 1st is just like you saw, and second is the simply way
you just type this above MySqlConnection conn = new MySqlConnection(getConnection);

string getConnection = "server = localhost; uid=root;database=Payroll ";


have you done ??
if ya, now press f5 on your kerboard :) is it all right ?

the result will be like this



okay, it is all. do you have any problem maybe ? you can ask your problem by leave comment :D
hope i can help you :p happy coding :D !!!!!









No comments:

Post a Comment