MySQL Connection and Usage with C#

Alright friends, to the MsSQL or Ms Access database connections and usage you are used to, let's add one more: MySQL; you may need to use MySQL because of the host you use, to exchange data with your website (php), or for some other reason. If you don't know how to do it with C#, or you know but have forgotten, here you go, let's go through it together to refresh your knowledge.
First of all, we need to download our absolute must-have, Connector/Net, which we will use as the MySQL driver ( http://dev.mysql.com/downloads/connector/net/ ) once the download is finished, we install the .NET connector and return to our project.
In our project we click Add Reference and go to the .NET tab, then find and add the MySql.Data reference.
After that, we import the references so we can use them.
using MySql.Data; using MySql.Data.MySqlClient;
now let's move on to how we make the server connection with MySQL
public MySqlConnection mysqlbaglan= new MySqlConnection("Server=localhost;Database=blog;Uid=root;Pwd='';");
I declared it as public here so that I can easily use it in the other forms as well. However, on some servers this connection definition alone is not enough and gives an error; for that, it is enough to adjust the definition as follows.
public MySqlConnection mysqlbaglan= new MySqlConnection("Server=localhost;Database=blog;Uid=root;Pwd='';Encrypt=false;AllowUserVariables=True;UseCompression=True");
Now we have established our MySQL connection; if we take a look at how it is used;
private void Form1_Load(object sender, EventArgs e)
{
try
{
mysqlbaglan.Open();
if (mysqlbaglan.State != ConnectionState.Closed)
{
MessageBox.Show("Connection Established Successfully");
}
else
{
MessageBox.Show("Unfortunately the Connection Could Not Be Established...!");
}
}
catch (Exception err)
{
MessageBox.Show("Error! " + err.Message, "An Error Occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
If we take a look at our code in full
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace mysql_ornek
{
public partial class Form1 : Form
{
public MySqlConnection mysqlbaglan= new MySqlConnection("Server=localhost;Database=blog;Uid=root;Pwd='';Encrypt=false;AllowUserVariables=True;UseCompression=True;chartset=utf8");// by adding charset we have also fixed the Turkish character problem.
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
mysqlbaglan.Open(); //we run the definition we created so that it gets opened
if (mysqlbaglan.State != ConnectionState.Closed) // we check the state of the definition, whether it is connected or not
{
MessageBox.Show("Connection Established Successfully"); // if it is connected, the operations here are carried out
}
else
{
MessageBox.Show("Unfortunately the Connection Could Not Be Established...!"); // if it is not connected, the operations here are carried out
}
}
catch (Exception err)
{
MessageBox.Show("Error! " + err.Message, "An Error Occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
I hope it has been useful, happy coding, friends...