Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data.SqlClient;
- using System.Data;
- using System.Globalization;
- namespace Missed_Lessons
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Connection path to our server and database.
- string connect = @"Data Source=curiouspc\sqlexpress;Initial Catalog=DataBase1;Integrated Security=True";
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- Console.Write("Do you want to add a record to the database?(");
- Console.ForegroundColor = ConsoleColor.Red;
- Console.Write("Y/N");
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write("): ");
- Console.ForegroundColor = ConsoleColor.DarkGreen;
- string agree_1 = Console.ReadLine();
- Console.ForegroundColor = ConsoleColor.White;
- //User adding method.
- if (agree_1 == "Y")
- {
- // String input and convert to DateTime. *Date
- Console.Write("Enter date (MM/DD/YYYY): ");
- string date_str = Console.ReadLine();
- DateTime date = Convert.ToDateTime(date_str);
- // String input. *Surname
- Console.Write("Enter surname: ");
- string surname = Console.ReadLine();
- // String input. *Group
- Console.Write("Enter group name: ");
- string group = Console.ReadLine();
- // String input and convert to integer. *Year of Birth
- Console.Write("Enter year of birth (only numbers): ");
- string str_age = Console.ReadLine();
- int year_of_birth = Convert.ToInt32(str_age);
- // String input and convert to integer. *Programming
- Console.Write("Enter amount of missed lessons for programming (only numbers): ");
- string str_prog = Console.ReadLine();
- int programming = Convert.ToInt32(str_prog);
- // String input and convert to integer. *Mathematics
- Console.Write("Enter amount of missed lessons for math (only numbers): ");
- string str_math = Console.ReadLine();
- int mathematics = Convert.ToInt32(str_math);
- // String input and convert to integer. *Physics
- Console.Write("Enter amount of missed lessons for physics: ");
- string str_physics = Console.ReadLine();
- int physics = Convert.ToInt32(str_physics);
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- string sqlExpression = "INSERT INTO MissLess (Date, Surname, [Group], [Year of Birth], Programming, Mathematics, Physics) VALUES (@date, @surname, @group, @year_of_birth, @programming, @mathematics, @physics)";
- using (SqlConnection connection = new SqlConnection(connect))
- {
- connection.Open();
- SqlCommand request = new SqlCommand(sqlExpression, connection);
- //Creating and adding new paramaters to request.
- //*Date
- SqlParameter sqlparam_date = new SqlParameter("@date", date);
- request.Parameters.Add(sqlparam_date);
- //*Surname
- SqlParameter sqlparam_surname = new SqlParameter("@surname", surname);
- request.Parameters.Add(sqlparam_surname);
- //*Group
- SqlParameter sqlparam_group = new SqlParameter("@group", group);
- request.Parameters.Add(sqlparam_group);
- //*Year of Birth
- SqlParameter sqlparam_age = new SqlParameter("@year_of_birth", year_of_birth);
- request.Parameters.Add(sqlparam_age);
- //*Programming
- SqlParameter sqlparam_prog = new SqlParameter("@programming", programming);
- request.Parameters.Add(sqlparam_prog);
- //*Mathematics
- SqlParameter sqlparam_math = new SqlParameter("@mathematics", mathematics);
- request.Parameters.Add(sqlparam_math);
- //*Physics
- SqlParameter sqlparam_physics = new SqlParameter("@physics", physics);
- request.Parameters.Add(sqlparam_physics);
- int num = request.ExecuteNonQuery();
- Console.WriteLine("\nNew records added to the database: {0}\n", num);
- }
- }
- Console.Write("Do you want to check information for missed lessons?(");
- Console.ForegroundColor = ConsoleColor.Red;
- Console.Write("Y/N");
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write("): ");
- Console.ForegroundColor = ConsoleColor.DarkGreen;
- string agree_2 = Console.ReadLine();
- Console.ForegroundColor = ConsoleColor.White;
- //Output specific user.
- if (agree_2 == "Y")
- {
- //some variables
- int sk_maxmiss_program = 0;
- int sk_maxmiss_math = 0;
- int sk_maxmiss_phys = 0;
- //Max number of PROGRAMMING column
- Console.WriteLine("\nOverall Student Statistic:");
- string sqlExpression_program = "SELECT MAX(Programming) FROM MissLess";
- using (SqlConnection connection = new SqlConnection(connect))
- {
- connection.Open();
- SqlCommand command = new SqlCommand(sqlExpression_program, connection);
- object maxMiss_program = command.ExecuteScalar();
- Console.WriteLine("(MAX)Missed PROGRAMMING lessons: {0} by student.", maxMiss_program);
- sk_maxmiss_program = Convert.ToInt32(maxMiss_program);
- }
- //Max number of MATHEMATICS column
- string sqlExpression_Math = "SELECT MAX(Mathematics) FROM MissLess";
- using (SqlConnection connection = new SqlConnection(connect))
- {
- connection.Open();
- SqlCommand command = new SqlCommand(sqlExpression_Math, connection);
- object maxMiss_math = command.ExecuteScalar();
- Console.WriteLine("(MAX)Missed MATHEMATICS lessons: {0} by student.", maxMiss_math);
- sk_maxmiss_math = Convert.ToInt32(maxMiss_math);
- }
- //Max number of PHYSICS column
- string sqlExpression_Phys = "SELECT MAX(Physics) FROM MissLess";
- using (SqlConnection connection = new SqlConnection(connect))
- {
- connection.Open();
- SqlCommand command = new SqlCommand(sqlExpression_Phys, connection);
- object maxMiss_phys = command.ExecuteScalar();
- Console.WriteLine("(MAX)Missed PHYSICS lessons: {0} by student.", maxMiss_phys);
- sk_maxmiss_phys = Convert.ToInt32(maxMiss_phys);
- }
- //Max number of all 3 columns
- int maxmisses_overall_unfin = Math.Max(sk_maxmiss_program, sk_maxmiss_math);
- int maxmisses_total_ = Math.Max(maxmisses_overall_unfin, sk_maxmiss_phys);
- Console.WriteLine("(MAX) Missed Lessons - Overall: " + maxmisses_total_ + "\n");
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- string sqlExpression_datafinder = "SELECT Date,Surname,Programming,Mathematics,Physics FROM MissLess WHERE Programming=@maxmisses_total_ OR Mathematics=@maxmisses_total_ OR Physics=@maxmisses_total_"; //vaicājums
- using (SqlConnection connection = new SqlConnection(connect))
- {
- connection.Open();
- SqlCommand command = new SqlCommand(sqlExpression_datafinder, connection);
- //parameter to identify variable
- SqlParameter total_param = new SqlParameter("@maxmisses_total_", maxmisses_total_);
- command.Parameters.Add(total_param);
- SqlDataReader reader = command.ExecuteReader();
- if (reader.HasRows)
- {
- //test - Console.WriteLine("{0} \t {1} \t {2} \t {3} \t {4}", reader.GetName(0), reader.GetName(1), reader.GetName(2), reader.GetName(3), reader.GetName(4));
- while (reader.Read()) // lasīt datus pa rindām
- {
- object Date = reader.GetValue(0);
- object Surname = reader.GetValue(1);
- object Programming = reader.GetValue(2);
- object Mathe = reader.GetValue(3);
- object Phys = reader.GetValue(4);
- //Console.WriteLine("{0} \t {1} \t {2} \t {3} \t {4}", Date, Surname, Programming, Mathe, Phys);
- int prog_integer = Convert.ToInt32(Programming);
- int mathe_integer = Convert.ToInt32(Mathe);
- int phys_integer = Convert.ToInt32(Phys);
- if (prog_integer > mathe_integer)
- {
- if (prog_integer > phys_integer)
- {
- Console.WriteLine($"({{0}}) - {{1}} has {maxmisses_total_} missed programming lessons!", Date, Surname);
- }
- else
- Console.WriteLine($"({{0}}) - {{1}} has {maxmisses_total_} missed physics lessons!", Date, Surname);
- }
- else if (mathe_integer > prog_integer)
- {
- if (mathe_integer > phys_integer)
- {
- Console.WriteLine($"({{0}}) - {{1}} has {maxmisses_total_} missed mathematics lessons!", Date, Surname);
- }
- else
- Console.WriteLine($"({{0}}) - {{1}} has {maxmisses_total_} missed physics lessons!", Date, Surname);
- }
- else
- Console.WriteLine($"({{0}}) - {{1}} has {maxmisses_total_} missed physics lessons!", Date, Surname);
- }
- }
- reader.Close();
- }
- Console.ReadLine();
- }
- else
- Console.WriteLine("\nConsole will close in 10 seconds...");
- System.Threading.Thread.Sleep(10000);
- Environment.Exit(0);
- ;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement