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 111
| 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;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private MySqlConnection ConnectionRessource;
private MySqlCommand clientSelect;
private MySqlCommand insertDonnees;
public Form1()
{
InitializeComponent();
SqlConnection();
}
public Boolean SqlConnection()
{
string DBName = "courtage";
string Server = "localhost";
string Login = "root";
string Password = "";
string connectionString = "Server=" + Server +
";Database=" + DBName +
";User ID=" + Login +
";Password=" + Password + ";";
try
{
ConnectionRessource = new MySqlConnection(connectionString);
toolStripStatusLabel1.Text = "Ouverture de la connection à la table" + DBName + ".";
ConnectionRessource.Open();
toolStripStatusLabel1.Text = "connection établie avec succès à la table " + DBName + ".";
return true;
}
catch (MySqlException myEx)
{
toolStripStatusLabel1.Text = "Error " + myEx.GetType() + " : Impossible de se connecter à la base de donnée.";
return false;
}
}
public Boolean selectClient()
{
//Création de la commande
string selectCommand = "SELECT * FROM contact;";
clientSelect = new MySqlCommand(selectCommand); //Commande
try //Procédure de test
{
toolStripStatusLabel2.Text = "Liste des contacts chargé avec succès"; //Informations a l'utilisateur du succès
return true;
}
catch (MySqlException myEx) //Erreur
{
toolStripStatusLabel2.Text = "Error " + myEx.GetType() + " : Impossible de lister les contacts"; //Information à l'utilisateur de l'erreur
return false;
}
}
public Boolean insertDonnee()
{
string sName = txtName.Text;
string sFirstName = txtFirstName.Text;
MySqlCommand insertDonnees = new MySqlCommand();
insertDonnees.CommandText = "INSERT INTO contact(id, name, firstName) VALUES('', '" + sName + "', '" + sFirstName + "');";
try
{
insertDonnees.Connection = ConnectionRessource;
MySqlCommand insertExecuter = new MySqlCommand();
MySqlDataReader executer = insertDonnees.ExecuteReader();
toolStripStatusLabel1.Text = "Donnée ajouté avec succès à la table.";
executer.Close();
return true;
}
catch (MySqlException myEx)
{
MessageBox.Show("Error: " + myEx + "Impossible d'ajouté les données");
return false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
selectClient();
}
private void btnInsert_Click(object sender, EventArgs e)
{
insertDonnee();
}
}
} |