Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. namespace _03MinionNames
  2. {
  3.     using System;
  4.     using System.Data.SqlClient;
  5.  
  6.     public class StartUp
  7.     {
  8.         private const string DB_NAME = "MinionsDB";
  9.  
  10.         private static string connectionString = @$"Server=.\SQLEXPRESS;Database={DB_NAME};Integrated Security=true";
  11.  
  12.         public static void Main(string[] args)
  13.         {
  14.             int villainId = int.Parse(Console.ReadLine());
  15.  
  16.             SqlConnection connection = new SqlConnection(connectionString);
  17.  
  18.             connection.Open();
  19.  
  20.             using (connection)
  21.             {
  22.                 string queryText = @"SELECT [Name] FROM Villains WHERE Id = @Id
  23.  
  24.                                     SELECT ROW_NUMBER() OVER(ORDER BY [Name]) AS RowNumber,
  25.                                            m.[Name],
  26.                                            m.Age
  27.                                       FROM MinionsVillains AS mv
  28.                                            JOIN Minions AS m ON m.Id = mv.MinionId
  29.                                      WHERE mv.VillainId = @Id
  30.                                   ORDER BY m.[Name]";
  31.  
  32.                 using SqlCommand sqlCommand = new SqlCommand(queryText, connection);
  33.  
  34.                 sqlCommand.Parameters.AddWithValue("@Id", villainId);
  35.  
  36.                 string villainName = (string)sqlCommand.ExecuteScalar();
  37.  
  38.                 if (villainName == null)
  39.                 {
  40.                     Console.WriteLine($"No villain with ID {villainId} exists in the database.");
  41.                     return;
  42.                 }
  43.  
  44.                 SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
  45.  
  46.                 using (sqlDataReader)
  47.                 {
  48.                     while (sqlDataReader.Read())
  49.                     {
  50.                         Console.WriteLine($"Villain: {villainName}");
  51.  
  52.                         int counter = 1;
  53.                         string minionName = (string)sqlDataReader["m.Name"];
  54.  
  55.                         if (minionName != null)
  56.                         {
  57.                             int minionAge = (int)sqlDataReader["m.Age"];
  58.  
  59.                             Console.WriteLine($"{counter}. {minionName} {minionAge}");
  60.                             counter++;
  61.                         }
  62.                         else
  63.                         {
  64.                             Console.WriteLine("(no minions)");
  65.                         }
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement