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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
   | using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlTypes;
 
namespace Client_PTI_ECOLE
{
    public partial class ajoutProf : Form
    {
        public static SqlCommand cmd, requete, cmdCreeProf, cmdAssocieClasse, cmdSelectLastProf, cmdSelectNumClasse;
        public static SqlDataReader numProf, numClasse;
        public static SqlConnection Connexion;
        private int numprof, numclasse;
 
        public ajoutProf()
        {
            InitializeComponent();
            listBox1.DataSource = BD.lireClasse();
            listBox1.ValueMember = "Numero";
            listBox1.DisplayMember = "Libelle";
            Connexion = BD.connexion();
        }
 
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
 
        private void btnValider_Click(object sender, EventArgs e)
        {
            if (nomProf.Text != "" || prenomProf.Text != "")
            {
                // ============================================================
				// Définition des objets Command pour les procédures stockées :
				// ============================================================
 
				// ============================
				// Pour la création du Prof :
				// ============================
				// Objet SqlCommand
				cmdCreeProf = new SqlCommand("sp_insert_prof", Connexion);
				// On indique que l'on souhaite utiliser une procédure stockée
				cmdCreeProf.CommandType = CommandType.StoredProcedure;
				// On indique les paramètres
				cmdCreeProf.Parameters.Add("@nom", SqlDbType.VarChar).Value = nomProf.Text;
				cmdCreeProf.Parameters.Add("@prenom", SqlDbType.VarChar).Value = prenomProf.Text;
				// On donne le nom de cette procédure stockée
				cmdCreeProf.CommandText = "sp_insert_prof";
 
				// ====================================
				// Pour l'ajout des classes associées au prof :
				// ====================================
				cmdAssocieClasse = new SqlCommand("sp_insert_prof_classe", Connexion);
				// On indique que l'on souhaite utiliser une procédure stockée
				cmdAssocieClasse.CommandType = CommandType.StoredProcedure;
				// On donne le nom de cette procédure stockée
				cmdAssocieClasse.CommandText = "sp_insert_prof_classe";
 
				// =============
				// Transaction :
				// =============
 
				// --------------------------------------------------------------
				// 1) Début de la transaction :
				// --------------------------------------------------------------
                SqlTransaction MaTransaction = Connexion.BeginTransaction();
 
				try
				{
					// --------------------------------------------------------------
					// 2) L'objet transaction est assigné à la propriété Transaction
					//    de chaque objet Command à exécuter :
					// --------------------------------------------------------------
 
					cmdCreeProf.Transaction			= MaTransaction;
					cmdAssocieClasse.Transaction	= MaTransaction;
 
					// --------------------------------------------------------------
					// 3) Exécution des commandes de la transaction :
					// --------------------------------------------------------------
 
					// Création du Prof :
					cmdCreeProf.ExecuteNonQuery();
 
					// Objet SqlCommand
					cmdSelectLastProf = new SqlCommand("sp_select_last_prof", Connexion);
                    // On associe l'objet à la transaction
                    cmdSelectLastProf.Transaction = MaTransaction;
					// On indique que l'on souhaite utiliser une procédure stockée
					cmdSelectLastProf.CommandType = CommandType.StoredProcedure;
					// On donne le nom de cette procédure stockée
					cmdSelectLastProf.CommandText = "sp_select_last_prof";
					// On exécute la procédure stockée
					numProf = cmdSelectLastProf.ExecuteReader();
					// On lit le premier et unique résultat
					numProf.Read();
                    numprof = numProf.GetInt16(0);
                    numProf.Close();
 
					// Association des classes à l'enseignant
					for ( int i=0; i < listBox2.Items.Count; i++ )
					{
						// On indique le paramètre pour l'enseignant
						cmdAssocieClasse.Parameters.Add("@num_ens", SqlDbType.VarChar).Value = numprof;
						// On indique le paramètre pour la classe
                        cmdAssocieClasse.Parameters.Add("@num_cla", SqlDbType.VarChar).Value = ((ClasseNUM_INT)(listBox2.Items[i])).Numero;
						// On exécute la procédure stockée
						cmdAssocieClasse.ExecuteNonQuery();
                        cmdAssocieClasse.Parameters.Clear();
					}
 
					MaTransaction.Commit();
					MessageBox.Show("Le nouvel enseignant a été créé.",	"Transaction achevée", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Connexion.Close();
                    btnCancel.Text = "Quitter";
                    btnValider.Enabled = false;
				}
 
				catch (Exception UneException)
				{
					// -------------------------------
					// Annulation de la transaction :
					// -------------------------------
 
					MaTransaction.Rollback();
 
					MessageBox.Show("L'erreur suivante s'est produite : " + UneException.Message
						+ "\nAucune donnée n'a été écrite dans la base.",
						"Erreur", MessageBoxButtons.OK, MessageBoxIcon.Stop);
					Connexion.Close();
				}
			}
            else
            {
                MessageBox.Show("Veuillez remplir tous les champs, merci");
            }
        }
 
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (listBox2.Items.IndexOf(listBox1.Text) > -1)
            {
                MessageBox.Show("La classe est déjà affectée à l'enseignant");
            }
            else
            {
                listBox2.Items.Add(listBox1.SelectedItem);
 
            }
        }
 
        private void btnSup_Click(object sender, EventArgs e)
        {
            listBox2.Items.RemoveAt(listBox2.Items.IndexOf(listBox2.Text));
        }
    }
} |