1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| /*
* ---------------------------------
* Auteur : SmashScharrer
* Date : 10/12/2018
* ---------------------------------
* V1.0 : Connexion BDD avec C#
*
* ---------------------------------
*
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Threading;
namespace BDD_MySQL
{
public partial class Form1 : Form
{
/* Variables */
string server, database, user, pwd, chaineConnexion;
Form1 index = new Form1();
Form2 requete = new Form2();
MySqlConnection connexion;
Thread thead1 = new Thread();
private void FormLoad(object sender, EventArgs e)
{
// Affichage TextBox Résultat
tbResultat.Text = "Saisissez les informations de connexion à la BDD : ";
tbResultat.BackColor = Color.Orange;
// Affichage TextBox de Connexion
tbServer.Text = "";
tbDatabase.Text = "";
tbUser.Text = "";
tbPWD.Text = "";
}
public Form1()
{
InitializeComponent();
}
/* Préambule */
/* Connexion BDD en Procédurale */
private void bConnexion(object sender, EventArgs e)
{
// Affectation chaine de caractère des TextBox
server = tbServer.Text;
database = tbDatabase.Text;
user = tbUser.Text;
pwd = tbPWD.Text;
// Chaîne de connexion
chaineConnexion = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + user + ";" + "PASSWORD=" + pwd + ";";
// Si Tentative de Connexion
if (btnConnexion.Text == "Connexion")
{
connexion = new MySqlConnection(chaineConnexion);
// Si Connxion réussie
try
{
connexion.Open();
btnConnexion.Text = "Déconnexion";
tbResultat.Text = "Connexion à la BDD réussie !";
tbResultat.BackColor = Color.Green;
Thread
requete.Show();
index.Hide();
}
// Sinon si Connexion échouée
catch(Exception ex)
{
tbResultat.Text = "Erreur : " + ex.ToString();
tbResultat.BackColor = Color.Red;
}
}
// Sinon
else
{
try
{
connexion.Close();
btnConnexion.Text = "Connexion";
tbResultat.Text = "Saisissez les informations de connexion à la BDD : ";
tbResultat.BackColor = Color.Orange;
}
catch(Exception ex)
{
tbResultat.Text = "Erreur : " + ex.ToString();
tbResultat.BackColor = Color.Red;
}
}
}
}
} |