Connect to a MySQL Server - RMB Scripting
You appear not to have an account with us, Click HERE to register an account...
RMB Scripting
Navigation
  Home
  Contact
  FAQ
  Forum
  Topsites
  Members
  Affiliates
  Arcade <-- New!
  IoxHost
Downloads
  Templates
  Scripts
  Fonts
  Image Sets
  Software
  Other
 
Tutorials
  .htaccess
  Photoshop
  C/C++
  CSS
  Macromedia Flash
  MySQL
  PHP
  PHP - User System
  HTML
  Javascript
  Macintosh
  Macromedia Fireworks
  Visual Basic
  Visual C#
  Windows
  Python
  Other
 
Latest Tutorials
Get the date of every ...
If...Else on one line ...
Drop Calendar Function
Single VS Double
Understanding Variables
Latest Comments
Simple Comment System
Simple Comment System
Simple Comment System
Simple Comment System
Private Message System
Stats
Total Members: 585 [231]
Total Tutorials: 276
Newsest User: zaithstery
Todays Unique Hits: 71
Users Online: 0
 
9 Guests
Poll
Should we change our name?
Hmmmm, Yes!!
Maybe
Depends on what to
NO!
Main Content
Tutorial: Connect to a MySQL Server By ShadowMage
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.



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"



After you do that go to the Browse tab and open
   
CODE:
C:\Program Files\MySQL\MySQL Connector Net 5.2.5\Binaries\.NET 2.0


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:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


Add After
   
CODE:
using MySql.Data.MySqlClient;


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:
Name: lblHost
Text: MySQL Host


Label 2
   
CODE:
Name: lblUser
Text: MySQL User


Label 3
   
CODE:
Name: lblPass
Text: MySQL Pass


Textbox 1
   
CODE:
Name: txtHost


Textbox 2
   
CODE:
Name: txtUser


Textbox 3
   
CODE:
Name: txtPass
PasswordChar: *


Button 1
   
CODE:
Name: cmdTestConnection
Text: Test Connection


This here is my outcome ;)


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

Double click your Test Connection button and under

   
CODE:
public frmMain()
{
InitializeComponent();
}


Hit enter one more time and place the following lines of code.
   
CODE:
private String Host;
private String User;
private String Pass;




Go back into your void of pressing the button and add the following:
   
CODE:
this.Host = txtHost.Text;
this.User = txtUser.Text;
this.Pass = txtPass.Text;
MySqlConnection MyCon = new MySqlConnection("server=" + this.Host + "; user id=" + this.User + "; password=" + this.Pass + "; pooling=false;");
try
{
MyCon.Open();
MessageBox.Show("MySQL Connection Succeeded", "Success");
}
catch (MySqlException ex)
{
MessageBox.Show("MySQL Connection Failed! " + ex.Message, "Error");
}


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:



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 ;)
Difficulty: Moderate
Views: 2920
Rating:
Comments
Posted on Monday 19th January 2009 at 12:11 AM
aaron1988
Nice tutorial
Notice:
Remember to post long codes on our pastbin! - (http://rmb.pastebin.com/)
Add Comment
You must be logged in to post a comment.
Good Morning Guest
Username: 
Password: 
Remember Username
Links
RMB Arcade
$2.50 Resellers
Free Domains
Free Games
Affiliates