- tp : etudient avec language Dot.net - Licence Fondamentale d'Informatique
mercredi 22 mai 2013

tp : etudient avec language Dot.net










using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.OleDb;
namespace bdetudient
{
         class DataAccess
        {
          //   les fonctions sql 
         // requete  insert a la base de donnée   
            public static void AjouterEtudiant(OleDbConnection cnn, OleDbParameter parcin, OleDbParameter parnom, OleDbParameter parprenom, OleDbParameter pardate_naissance, OleDbParameter paradresse, OleDbParameter partelephone)
            {
                string S = "INSERT INTO ETUDIANT VALUES (?,?,?,?,?,?)";
                OleDbCommand cmd = new OleDbCommand(S, cnn);
                cmd.Parameters.Add(parcin);
                cmd.Parameters.Add(parnom);
                cmd.Parameters.Add(parprenom);
                cmd.Parameters.Add(pardate_naissance);
                cmd.Parameters.Add(paradresse);
                cmd.Parameters.Add(partelephone);
                cmd.ExecuteNonQuery();
                Console.WriteLine("ce bon");


            }

            // requete  insert a la base de donnée   
            public static void SupprimerEtudiant(OleDbConnection cnn, OleDbParameter parcin)
            {
                string strsql = "delete from etudiant where(cin=?)";
                OleDbCommand cmd = new OleDbCommand(strsql, cnn);
                cmd.Parameters.Add(parcin);
                cmd.ExecuteNonQuery();
                Console.WriteLine("ce bon");

            }
         
             // requete  insert a la base de donnée   
            public static void modifierEtudiant(OleDbConnection cnn, OleDbParameter parcin, OleDbParameter parcin2)
            {
                string strsql = "UPDATE  etudiant set cin= ? where cin = ?";
                OleDbCommand cmd = new OleDbCommand(strsql, cnn);
                cmd.Parameters.Add(parcin2);
                cmd.Parameters.Add(parcin);
                 cmd.ExecuteNonQuery();
                Console.WriteLine("ce bon");

            }

            public static void afficherEtudiant(OleDbConnection cnn)
            {
                string strsql = "SELECT * FROM  ETUDIANT ";
                OleDbCommand cmd = new OleDbCommand(strsql, cnn);
                OleDbDataReader rd = cmd.ExecuteReader();
                string s; string s2; string s3; string s4; string s5;
                Int16 ci;
                int i = 0;
                if (rd != null)
                {
                    while (rd.Read())
                    {

                      
                                               s = (string)rd[1];
                        s2 = (string)rd[2];
                        s3 = (string)rd[3];
                        s4 = (string)rd[4];
                        s5 = (string)rd[5];
                        ci = (Int16)rd["cin"];
                               i += 1;
                             //  Console.WriteLine(ci);
                        Console.WriteLine("                      etudiant " + i+":");
                        Console.WriteLine("                                 CIN  :" + ci );
                        Console.WriteLine("                                 nom :" + s );
                        Console.WriteLine("                                 prenom :" + s2 );
                        Console.WriteLine("                                 annee : " + s3);
                        Console.WriteLine("                                 adr : " + s4 );
                        Console.WriteLine("                                 tel :" + s5);
                               Console.WriteLine("             ");



                    }
              }
                Console.WriteLine("ce bon");
            }
          
             // appels de fonctions SQL
             // sasir ajouter
             public static void sasi_ajouter(OleDbConnection cnn)
            {
                Console.WriteLine(" ****** Ajouter Etudiant ******");

               
                string nom, prenom, date_naissance, adresse, telephone;
                int cin;
                cnn.Open();
                Console.WriteLine("| Donner le CIN :");
                cin = Int32.Parse(Console.ReadLine());
                OleDbParameter parcin = new OleDbParameter("cin", cin);
                Console.WriteLine("| Donner le Nom :");
                nom = Console.ReadLine();
                OleDbParameter parnom = new OleDbParameter("nom", nom);
                Console.WriteLine("| Donner le Prenom :");
                prenom = Console.ReadLine();
                OleDbParameter parprenom = new OleDbParameter("prenom", prenom);
                Console.WriteLine("| Donner l'annee :");
                date_naissance = Console.ReadLine();
                OleDbParameter pardate_naissance = new OleDbParameter("annee", date_naissance);

                Console.WriteLine("| Donner l'adresse :");
                adresse = Console.ReadLine();
                OleDbParameter paradresse = new OleDbParameter("adr", adresse);
                Console.WriteLine("| Donner numero de telphone :");
                telephone = Console.ReadLine();
                OleDbParameter partelephone = new OleDbParameter("tel", telephone);
                AjouterEtudiant(cnn, parcin, parnom, parprenom, pardate_naissance, paradresse, partelephone);
                cnn.Close();

            }
             // sasir ajouter
            public static void suprimer(OleDbConnection cnn)
            {
                Console.WriteLine(" *********** Supprimer  Etudiant ***********");
                string cin;
                cnn.Open();
                Console.WriteLine("donne le cin ade l'etudiant que voulez vous supprimer :");
                cin = Console.ReadLine();
                OleDbParameter parcin = new OleDbParameter("cin", cin);
                SupprimerEtudiant(cnn, parcin);
                cnn.Close();

            }
             // modifier
            public static int testcin(OleDbConnection cnn, Int16 cini, OleDbParameter parcin)
            {
                string strsql = "SELECT cin FROM  ETUDIANT Where cin = ? ";
                OleDbCommand cmd = new OleDbCommand(strsql, cnn);
                OleDbDataReader rd = cmd.ExecuteReader();
               
                cmd.Parameters.Add(parcin);
                Console.WriteLine(cini);
              
                int c = 0;
                if (rd != null)
                {
                     c = cini;
                }

               if ( c == cini)
                    return (1);// n'exister pas
                else
                    return (-1);// exister
            }
            public static void modifier(OleDbConnection cnn)
            {
                Console.WriteLine(" *********** modifier  Etudiant ***********");
                cnn.Open();

                Console.WriteLine("donne le CIN a de l'etudiant que voulez vous modifier :");
                string cin = Console.ReadLine();
                OleDbParameter parcin = new OleDbParameter("cin",cin);
                Int16 cini = Int16.Parse(cin);
              //--------------------------
                //Console.WriteLine( cin);
                //string strsql = "SELECT cin FROM  ETUDIANT Where cin = ? ";
                //OleDbCommand cmd = new OleDbCommand(strsql, cnn);
                //cmd.Parameters.Add(parcin);

                //OleDbDataReader rd = cmd.ExecuteReader();
             
             //----------------------
                if (testcin(cnn, cini, parcin) == 1) // exister
                {
                    Console.WriteLine("donne la Nouvelle CIN a de l'etudiant :");
                    //string cin2 = Console.ReadLine();
                    //OleDbParameter parcin2 = new OleDbParameter("cin", cin2);
                    //if (testcin(cnn, cini, parcin2) == -1)
                    //               {
                    //                   modifierEtudiant(cnn, parcin, parcin2);
                    //                  Console.WriteLine("ce bon");
                    //                }
                    //               else
                    //               {
                    //              Console.WriteLine("cind déja utliser");
                    //                  }
                }
                else
                {
                    Console.WriteLine("cin n'exister pas ,verifier votre choix");
                }
                cnn.Close();

            }

         
            public static void afficher(OleDbConnection cnn)
            {
                Console.WriteLine(" ****** afficher Etudiant ******");
                cnn.Open();
                afficherEtudiant(cnn);
                cnn.Close();
            }

            public static void choix2()
            {
                Console.WriteLine("tapez '1' pour return Menu:");
                Console.WriteLine("tapez '0' pour quitter:");
                Console.WriteLine("tapez votre choix:");
                int Choix = Int32.Parse(Console.ReadLine());
                switch (Choix)
                {
                    case 1:
                        {
                            System.Console.Clear();
                            Menu();

                        }; break;
                  
                    case 0:
                        break;
                }
            }
            public static void Menu ()
            {

                string str = @"provider=Microsoft.ace.OleDb.12.0; Data source=C:\Users\ahmedoumezzine\Documents\Visual Studio 2012\Projects\bdetudient\Base de données1.accdb";
                OleDbConnection cnn = new OleDbConnection(str);
            
                Console.WriteLine("\t\t\t     Menu Principale \n\n\n");
                Console.WriteLine("tapez 1 pour ajouter un etudiant:");
                Console.WriteLine("tapez 2 pour supprimer un etudiant:");
                Console.WriteLine("tapez 3 pour modifier un etudiant:");
                Console.WriteLine("tapez 4 pour afficher un etudiant:");

                Console.WriteLine("tapez 0 pour quitter:");
                Console.WriteLine("tapez votre choix:");
                int Choix = Int32.Parse(Console.ReadLine());
                 switch (Choix)
                {
                    case 1:
                        {
                            System.Console.Clear();
                            sasi_ajouter(cnn);
                            Console.WriteLine("     ");
                            Console.WriteLine("   ");
                            choix2();

                        };break;
                            case 2:
                          {
                              System.Console.Clear();
                              suprimer(cnn);
                              Console.WriteLine("     ");
                              Console.WriteLine("   ");
                              choix2();
                              System.Console.Clear();
                        };break;
                     case 3:
                         {
                             System.Console.Clear();
                              modifier(cnn);
                              Console.WriteLine("     ");
                              Console.WriteLine("   ");
                              choix2();
                              System.Console.Clear();
                        };break;
                     case 4:
                         {
                             System.Console.Clear();
                             afficher(cnn);
                             Console.WriteLine("     ");
                             Console.WriteLine("   ");
                             choix2();
                             System.Console.Clear();
                         }; break;
                     case 0:
                         break;
                }
                            }


            static void Main(string[] args)
            {


                string str = @"provider=Microsoft.ace.OleDb.12.0; Data source=C:\Users\ahmedoumezzine\Documents\Visual Studio 2012\Projects\bdetudient\Base de données1.accdb";
                OleDbConnection cnn = new OleDbConnection(str);

                Console.WriteLine("\n\n\t\t  Bienvenus  \n\n\n\n");
                Console.WriteLine("\n\n\t\t Taper Sur 'Entrer' pour Demarrer \n\n\n\n");
                Console.ReadKey();
                System.Console.Clear();
                Menu();
            }
        }
    }


   


0 commentaires:

Enregistrer un commentaire

 
-