Vous êtes sur la page 1sur 4

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.OleDb;

namespace Etudiant
{
public partial class Form1 : Form
{
OleDbConnection con = new
OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
Application.StartupPath + "\\BD\\maBD.accdb");
OleDbDataAdapter oledbAdapter;
DataSet ds;
OleDbCommand cmd;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
afficher();
}

//Récupération tout le contenu de la table Access


public void afficher()
{
if (con.State.ToString().Equals("Closed"))
{
con.Open(); // Ouverture la connexion au fichier maBD.accdb
}
oledbAdapter = new OleDbDataAdapter();
ds = new DataSet();
//Gestion des exceptions au niveau de la sélection
try
{
//Préparer la requête de sélection dans l'objet oledbAdapter
oledbAdapter.SelectCommand = new OleDbCommand("SELECT * FROM etudiant
ORDER BY id ASC", con);
dataGridView1.DataSource = null; //Effacer le contenu du tableau
oledbAdapter.Fill(ds); // Récupérer le résultat de la requête
oledbAdapter.Dispose(); // Liberer les ressources mémoire utilisées par le
DbAdapter
dataGridView1.DataSource = ds.Tables[0]; // Remplire le tableau par le
résultat de la requête
//L'objet DataSet peut contenir plusieurs tableaux de résultats retournés,
dans notre cas
//on s'interesse juste à la premiere table de résultat Tales[0]
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
if (con.State.ToString().Equals("Open"))
{
con.Close(); //Fermeture de la connexion
}
}

private void afficherBtn_Click(object sender, EventArgs e)


{
afficher();
}

// Méthode pour vider le contenu des champs de saisie (TextBox)


public void vider()
{
idTxt.Text = "";
nomTxt.Text = "";
prenomTxt.Text = "";
}

private void dataGridView1_CellContentClick(object sender,


DataGridViewCellEventArgs e)
{
// Au moment du double clique on vide les champs et on récupère le contenu
// de la ligne sélectionnée
vider();
idTxt.Text = Convert.ToString(dataGridView1.CurrentRow.Cells[0].Value);
nomTxt.Text = Convert.ToString(dataGridView1.CurrentRow.Cells[1].Value);
prenomTxt.Text = Convert.ToString(dataGridView1.CurrentRow.Cells[2].Value);
//CurrentRow : La ligne courante
//dataGridView1 : Notre dataGridView dans l'interface Form1.cs
}

private void addBtn_Click(object sender, EventArgs e)


{
con.Open();
//---------- On récupère l'id du dernier enregistrement et on l'incrémente par
1 --------------

oledbAdapter.SelectCommand = new OleDbCommand("SELECT * FROM etudiant ORDER


BY id ASC", con);
oledbAdapter.Fill(ds);
oledbAdapter.Dispose();
string id = ds.Tables[0].Rows[ds.Tables[0].Rows.Count -
1].ItemArray[0].ToString();
//Tables[0] : Première table des résultats retournés
//ds.Tables[0].Rows.Count: Le nombre des enregistrement du tableau (Si on a 9
il retourne 10)
//Rows[x]: Pointé vers la ligne dont l'indice = x.
//ItemArray[0] : Retourne la valeur de la permière colonne (l'id de
l'etudiant)
//ToString() : Convertir la valeur de l'objet en chaine de caractère
//-----------------------------------Requete
d'insertion----------------------------------------
cmd = new OleDbCommand("INSERT INTO etudiant VALUES('" + (Convert.ToInt32(id)
+ 1) + "', '" + this.nomTxt.Text + "','" + this.prenomTxt.Text + "');", con);
//Gère l'exception de la requete de l'insertion
try
{
int temp = cmd.ExecuteNonQuery();
if (temp > 0)
{
// Vider les champs de saisie
vider();
// Récupérer touts les enregistrements y compris le dernier ajouté
afficher();
MessageBox.Show("Enregistré !");
}
else
{
MessageBox.Show("Non Enregistré");
}
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
con.Close();
}

private void updateBtn_Click(object sender, EventArgs e)


{
//Requete de modification
string req = "UPDATE etudiant SET nom='" + nomTxt.Text + "', prenom = '" +
prenomTxt.Text + "' WHERE id = " + idTxt.Text + ";";
cmd = new OleDbCommand(req, con);
try
{
con.Open();
int temp = cmd.ExecuteNonQuery();
if (temp > 0)
{
vider();
afficher();
MessageBox.Show("Modifications appliqués !");
}
else
{
MessageBox.Show("Erreur de modification !!");
}
con.Close();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
}

private void deleteBtn_Click(object sender, EventArgs e)


{
con.Open();
//Requete d'insertion

string req = "DELETE FROM etudiant WHERE id = " + idTxt.Text + ";";

cmd = new OleDbCommand(req, con);


try
{
if (MessageBox.Show("Supprimer l'enregistrement ?", "Confirmation",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
int temp = cmd.ExecuteNonQuery();
if (temp > 0)
{
vider();
afficher();
}
else
{
MessageBox.Show("Erreur de suppression !!");
}
}
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
con.Close();
}

private void QuitBtn_Click(object sender, EventArgs e)


{
Application.Exit();
}

}
}

Vous aimerez peut-être aussi