Ejemplo n.º 1
0
        public static int GetSubjectID(string subject, Verification verification)
        {
            SqlConnection connection = new SqlConnection(DataAccess.SQLCONNECTIONSTRING);

            connection.Open();

            int        subjectId     = 0;
            SqlCommand takeSubjectId = connection.CreateCommand();

            takeSubjectId.CommandText = "SELECT IDSubject FROM Subject WHERE NameSubject = @sujet";
            takeSubjectId.Parameters.AddWithValue("@sujet", subject);

            try
            {
                if (takeSubjectId.ExecuteScalar() == null)
                {
                    Console.WriteLine("Ce sujet n'existe pas");
                    Console.WriteLine();
                    Console.WriteLine("Vous voulez ajouter un nouveau sujet ?");
                    bool a = false;
                    while (!a)
                    {
                        Console.WriteLine("O/N");
                        ConsoleKeyInfo resultat = Console.ReadKey();
                        Console.Clear();
                        switch (resultat.Key)
                        {
                        case ConsoleKey.O:
                            Console.Write("Sujet : ");
                            string sujet = Console.ReadLine();
                            sujet = TestException.CheckTextInfo(sujet);

                            SqlCommand firstInsert = connection.CreateCommand();
                            firstInsert.CommandText = "INSERT INTO Subject (NameSubject) VALUES (@sujet)";
                            firstInsert.Parameters.AddWithValue("@sujet", sujet);

                            try
                            {
                                firstInsert.ExecuteNonQuery();
                                Console.WriteLine("Le sujet {0} a été ajouté! \n", sujet);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Une erreur s'est produite: " + ex.Message);
                                Console.WriteLine("Type d'erreur: " + ex.GetType());
                                Console.WriteLine("Veuillez recommencer..");
                                MainMenu.ShowMainMenu(verification);
                                connection.Close();
                            }
                            break;

                        case ConsoleKey.N:
                            MainMenu.ShowMainMenu(verification);
                            break;
                        }
                        a = true;
                    }
                }
                else
                {
                    subjectId = (int)takeSubjectId.ExecuteScalar();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Erreur : " + ex.Message);
                Console.WriteLine("Type d'erreur: " + ex.GetType());
                Console.WriteLine("Pile : " + ex.StackTrace);
                MainMenu.ShowMainMenu(verification);
            }

            connection.Close();
            return(subjectId);
        }