Advertisement
Xyfer

Sistēmu programmēšana PB2

Nov 29th, 2021
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.34 KB | None | 0 0
  1. using System;
  2. using System.Data.SqlClient;
  3. using System.Data;
  4. using System.Globalization;
  5.  
  6. namespace Missed_Lessons
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             // Connection path to our server and database.
  13.             string connect = @"Data Source=curiouspc\sqlexpress;Initial Catalog=DataBase1;Integrated Security=True";
  14.             /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  15.  
  16.            
  17.             Console.Write("Do you want to add a record to the database?(");
  18.             Console.ForegroundColor = ConsoleColor.Red;
  19.             Console.Write("Y/N");
  20.             Console.ForegroundColor = ConsoleColor.White;
  21.             Console.Write("): ");
  22.  
  23.             Console.ForegroundColor = ConsoleColor.DarkGreen;
  24.             string agree_1 = Console.ReadLine();
  25.             Console.ForegroundColor = ConsoleColor.White;
  26.  
  27.             //User adding method.
  28.             if (agree_1 == "Y")
  29.             {
  30.                 // String input and convert to DateTime. *Date
  31.                 Console.Write("Enter date (MM/DD/YYYY): ");
  32.                 string date_str = Console.ReadLine();
  33.                 DateTime date = Convert.ToDateTime(date_str);
  34.  
  35.                 // String input. *Surname
  36.                 Console.Write("Enter surname: ");
  37.                 string surname = Console.ReadLine();
  38.  
  39.                 // String input. *Group
  40.                 Console.Write("Enter group name: ");
  41.                 string group = Console.ReadLine();
  42.  
  43.                 // String input and convert to integer. *Year of Birth
  44.                 Console.Write("Enter year of birth (only numbers): ");
  45.                 string str_age = Console.ReadLine();
  46.                 int year_of_birth = Convert.ToInt32(str_age);
  47.  
  48.                 // String input and convert to integer. *Programming
  49.                 Console.Write("Enter amount of missed lessons for programming (only numbers): ");
  50.                 string str_prog = Console.ReadLine();
  51.                 int programming = Convert.ToInt32(str_prog);
  52.  
  53.                 // String input and convert to integer. *Mathematics
  54.                 Console.Write("Enter amount of missed lessons for math (only numbers): ");
  55.                 string str_math = Console.ReadLine();
  56.                 int mathematics = Convert.ToInt32(str_math);
  57.  
  58.                 // String input and convert to integer. *Physics
  59.                 Console.Write("Enter amount of missed lessons for physics: ");
  60.                 string str_physics = Console.ReadLine();
  61.                 int physics = Convert.ToInt32(str_physics);
  62.  
  63.                 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  64.  
  65.  
  66.                 string sqlExpression = "INSERT INTO MissLess (Date, Surname, [Group], [Year of Birth], Programming, Mathematics, Physics) VALUES (@date, @surname, @group, @year_of_birth, @programming, @mathematics, @physics)";
  67.  
  68.                 using (SqlConnection connection = new SqlConnection(connect))
  69.                 {
  70.                     connection.Open();
  71.                     SqlCommand request = new SqlCommand(sqlExpression, connection);
  72.  
  73.                     //Creating and adding new paramaters to request.
  74.  
  75.                     //*Date
  76.                     SqlParameter sqlparam_date = new SqlParameter("@date", date);
  77.                     request.Parameters.Add(sqlparam_date);
  78.  
  79.                     //*Surname
  80.                     SqlParameter sqlparam_surname = new SqlParameter("@surname", surname);
  81.                     request.Parameters.Add(sqlparam_surname);
  82.  
  83.                     //*Group
  84.                     SqlParameter sqlparam_group = new SqlParameter("@group", group);
  85.                     request.Parameters.Add(sqlparam_group);
  86.  
  87.                     //*Year of Birth
  88.                     SqlParameter sqlparam_age = new SqlParameter("@year_of_birth", year_of_birth);
  89.                     request.Parameters.Add(sqlparam_age);
  90.  
  91.                     //*Programming
  92.                     SqlParameter sqlparam_prog = new SqlParameter("@programming", programming);
  93.                     request.Parameters.Add(sqlparam_prog);
  94.  
  95.                     //*Mathematics
  96.                     SqlParameter sqlparam_math = new SqlParameter("@mathematics", mathematics);
  97.                     request.Parameters.Add(sqlparam_math);
  98.  
  99.                     //*Physics
  100.                     SqlParameter sqlparam_physics = new SqlParameter("@physics", physics);
  101.                     request.Parameters.Add(sqlparam_physics);
  102.  
  103.                     int num = request.ExecuteNonQuery();
  104.                     Console.WriteLine("\nNew records added to the database: {0}\n", num);
  105.                 }
  106.             }
  107.            
  108.  
  109.             Console.Write("Do you want to check information for missed lessons?(");
  110.             Console.ForegroundColor = ConsoleColor.Red;
  111.             Console.Write("Y/N");
  112.             Console.ForegroundColor = ConsoleColor.White;
  113.             Console.Write("): ");
  114.             Console.ForegroundColor = ConsoleColor.DarkGreen;
  115.             string agree_2 = Console.ReadLine();
  116.             Console.ForegroundColor = ConsoleColor.White;
  117.  
  118.             //Output specific user.
  119.             if (agree_2 == "Y")
  120.             {
  121.                 //some variables
  122.                 int sk_maxmiss_program = 0;
  123.                 int sk_maxmiss_math = 0;
  124.                 int sk_maxmiss_phys = 0;
  125.  
  126.                 //Max number of PROGRAMMING column
  127.                 Console.WriteLine("\nOverall Student Statistic:");
  128.                 string sqlExpression_program = "SELECT MAX(Programming) FROM MissLess";
  129.                 using (SqlConnection connection = new SqlConnection(connect))
  130.                 {
  131.                     connection.Open();
  132.                     SqlCommand command = new SqlCommand(sqlExpression_program, connection);
  133.                     object maxMiss_program = command.ExecuteScalar();
  134.  
  135.                     Console.WriteLine("(MAX)Missed PROGRAMMING lessons: {0} by student.", maxMiss_program);
  136.                     sk_maxmiss_program = Convert.ToInt32(maxMiss_program);
  137.                    
  138.                 }
  139.  
  140.                 //Max number of MATHEMATICS column
  141.                 string sqlExpression_Math = "SELECT MAX(Mathematics) FROM MissLess";
  142.                 using (SqlConnection connection = new SqlConnection(connect))
  143.                 {
  144.                     connection.Open();
  145.                     SqlCommand command = new SqlCommand(sqlExpression_Math, connection);
  146.                     object maxMiss_math = command.ExecuteScalar();
  147.  
  148.                     Console.WriteLine("(MAX)Missed MATHEMATICS lessons: {0} by student.", maxMiss_math);
  149.                     sk_maxmiss_math = Convert.ToInt32(maxMiss_math);
  150.                 }
  151.  
  152.                 //Max number of PHYSICS column
  153.                 string sqlExpression_Phys = "SELECT MAX(Physics) FROM MissLess";
  154.                 using (SqlConnection connection = new SqlConnection(connect))
  155.                 {
  156.                     connection.Open();
  157.                     SqlCommand command = new SqlCommand(sqlExpression_Phys, connection);
  158.                     object maxMiss_phys = command.ExecuteScalar();
  159.  
  160.                     Console.WriteLine("(MAX)Missed PHYSICS lessons: {0} by student.", maxMiss_phys);
  161.                     sk_maxmiss_phys = Convert.ToInt32(maxMiss_phys);
  162.  
  163.                 }
  164.  
  165.                 //Max number of all 3 columns
  166.                 int maxmisses_overall_unfin = Math.Max(sk_maxmiss_program, sk_maxmiss_math);
  167.                 int maxmisses_total_ = Math.Max(maxmisses_overall_unfin, sk_maxmiss_phys);
  168.  
  169.                 Console.WriteLine("(MAX) Missed Lessons - Overall: " + maxmisses_total_ + "\n");
  170.  
  171.                 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  172.  
  173.                 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
  174.                 using (SqlConnection connection = new SqlConnection(connect))
  175.                 {
  176.                     connection.Open();
  177.                     SqlCommand command = new SqlCommand(sqlExpression_datafinder, connection);
  178.                     //parameter to identify variable
  179.                     SqlParameter total_param = new SqlParameter("@maxmisses_total_", maxmisses_total_);
  180.                     command.Parameters.Add(total_param);
  181.  
  182.                     SqlDataReader reader = command.ExecuteReader();
  183.  
  184.  
  185.  
  186.  
  187.                     if (reader.HasRows)
  188.                     {
  189.                         //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));
  190.                         while (reader.Read()) // lasīt datus pa rindām
  191.                         {
  192.                             object Date = reader.GetValue(0);
  193.                             object Surname = reader.GetValue(1);
  194.                             object Programming = reader.GetValue(2);
  195.                             object Mathe = reader.GetValue(3);
  196.                             object Phys = reader.GetValue(4);
  197.  
  198.  
  199.                             //Console.WriteLine("{0} \t {1} \t {2} \t {3} \t {4}", Date, Surname, Programming, Mathe, Phys);
  200.  
  201.                             int prog_integer = Convert.ToInt32(Programming);
  202.                             int mathe_integer = Convert.ToInt32(Mathe);
  203.                             int phys_integer = Convert.ToInt32(Phys);
  204.  
  205.  
  206.                             if (prog_integer > mathe_integer)
  207.                             {
  208.                                 if (prog_integer > phys_integer)
  209.                                 {
  210.                                     Console.WriteLine($"({{0}}) - {{1}} has {maxmisses_total_} missed programming lessons!", Date, Surname);
  211.                                 }
  212.                                 else
  213.                                     Console.WriteLine($"({{0}}) - {{1}} has {maxmisses_total_} missed physics lessons!", Date, Surname);
  214.  
  215.                             }
  216.                             else if (mathe_integer > prog_integer)
  217.                             {
  218.                                 if (mathe_integer > phys_integer)
  219.                                 {
  220.                                     Console.WriteLine($"({{0}}) - {{1}} has {maxmisses_total_} missed mathematics lessons!", Date, Surname);
  221.                                 }
  222.                                 else
  223.                                     Console.WriteLine($"({{0}}) - {{1}} has {maxmisses_total_} missed physics lessons!", Date, Surname);
  224.                             }
  225.                             else
  226.                                 Console.WriteLine($"({{0}}) - {{1}} has {maxmisses_total_} missed physics lessons!", Date, Surname);
  227.                         }
  228.                     }
  229.                     reader.Close();
  230.                 }
  231.  
  232.  
  233.  
  234.                
  235.  
  236.  
  237.  
  238.  
  239.                 Console.ReadLine();
  240.  
  241.  
  242.  
  243.             }
  244.             else
  245.                 Console.WriteLine("\nConsole will close in 10 seconds...");
  246.                 System.Threading.Thread.Sleep(10000);
  247.                 Environment.Exit(0);
  248.             ;
  249.         }
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement