Advertisement
StoyanGrigorov

8. IncreaseMinionsAge

Feb 25th, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data.SqlClient;
  7.  
  8. namespace IncreaseMinionsAge
  9. {
  10.     class IncreaseMinionsAge
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Console.Write("Enter Ids separated by space: ");
  15.             List<int> minionIds = Console.ReadLine().Split().Select(int.Parse).ToList();
  16.  
  17.             SqlConnection connection = new SqlConnection(@"
  18.                                    Server=.\SQLEXPRESS;" +
  19.                                     "Database=MinionsDB;" +
  20.                                     "Integrated Security=true");
  21.  
  22.  
  23.             connection.Open();
  24.  
  25.             using (connection)
  26.             {
  27.                 string updateQuery = $@"UPDATE Minions
  28.                                       SET Age = Age + 1, Name = LOWER(Name)
  29.                                       WHERE Id IN ({string.Join(", ", minionIds)})";
  30.  
  31.                 SqlCommand updateCmd = new SqlCommand(updateQuery, connection);
  32.  
  33.                 updateCmd.ExecuteNonQuery();
  34.  
  35.  
  36.                 string resultQuery = $@"SELECT m.Name, m.Age FROM Minions AS m WHERE m.Id IN ({string.Join(", ", minionIds)})";
  37.                 SqlCommand getResultsCmd = new SqlCommand(resultQuery, connection);
  38.  
  39.                 SqlDataReader reader = getResultsCmd.ExecuteReader();
  40.  
  41.  
  42.                 Console.WriteLine(" Minion name | Age ");
  43.                 Console.WriteLine("-------------|-----");
  44.                 while (reader.Read())
  45.                 {
  46.                     Console.WriteLine($"{reader[0], 12} |{reader[1], 3}");
  47.                 }
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement