Advertisement
Guest User

VillianNames-ForForum

a guest
Jun 27th, 2020
111
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. string ConnectionString = "Server=.;Database=MinionsDB;Integrated Security=true";
  2. using SqlConnection sqlConnection = new SqlConnection(ConnectionString);
  3. sqlConnection.Open();
  4.  
  5. string getVillainsWithMoreThanThreeeMinionsQuerryText =@"
  6. SELECT v.Name, COUNT(mv.VillainId) AS MinionsCount
  7. FROM Villains AS v
  8. JOIN MinionsVillains AS mv ON v.Id = mv.VillainId
  9. GROUP BY v.Id, v.Name
  10. HAVING COUNT(mv.VillainId) > 3
  11. ORDER BY COUNT(mv.VillainId)";
  12. using SqlCommand getVillainsWithMoreThanThreeeMinionsCommand =
  13. new SqlCommand(getVillainsWithMoreThanThreeeMinionsQuerryText, sqlConnection);
  14.  
  15. using SqlDataReader reader = getVillainsWithMoreThanThreeeMinionsCommand.ExecuteReader();
  16.  
  17. StringBuilder sb = new StringBuilder();
  18.  
  19. while (reader.Read())
  20. {
  21. string villainName = reader["Name"].ToString();
  22. string countOfMinions = reader["MinionsCount"].ToString();
  23. sb.AppendLine($"{villainName} - {countOfMinions}");
  24. }
  25.  
  26. Console.WriteLine(sb.ToString().TrimEnd());
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement