-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoachForm.cs
171 lines (146 loc) · 8.66 KB
/
CoachForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System.Data;
using Microsoft.Data.SqlClient;
namespace NCAAProject
{
public partial class CoachForm : Form
{
public CoachForm()
{
InitializeComponent();
//ADD COACHES TO DROP DOWN LISTS
string coachListSql = "SELECT \r\n CONCAT(FirstName, ' ', LastName) AS FullName\r\nFROM \r\n NCAA.HeadCoach;";
AddCoachesToDropDown(coachListSql);
}
private void compareButton_Click(object sender, EventArgs e)
{
DisplayCoachInfo(1);
DisplayCoachInfo(2);
}
private void AddCoachesToDropDown(string query)
{
try
{
SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder();
cb.UserID = "luke3802";
cb.Password = "55Warriors2020";
cb.DataSource = "mssql.cs.ksu.edu";
cb["Database"] = "luke3802";
cb.TrustServerCertificate = true;
SqlDataAdapter adapter = new SqlDataAdapter();
using (SqlConnection connection = new SqlConnection(cb.ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(query, connection))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string fullName = reader.GetString(0);
dropDownList1.Items.Add(fullName);
dropDownList2.Items.Add(fullName);
}
}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
private void DisplayCoachInfo(int index)
{
if(index == 1)
{
string coach1 = dropDownList1.SelectedItem.ToString();
string[] name1 = coach1.Split(' ');
string firstCoachQuery = String.Format(
"SELECT\r\n CoachA.FirstName,\r\n CoachA.LastName,\r\n CollegeA.[Name] AS CollegeName,\r\n COUNT(CASE WHEN WinningCoach.CoachID = CoachA.CoachID THEN 1 END) AS TotalWins,\r\n COUNT(CASE WHEN LosingCoach.CoachID = CoachA.CoachID THEN 1 END) AS TotalLosses,\r\n COUNT(CASE WHEN WinningTeam.CoachID = CoachA.CoachID AND Game.[Round] = 2 THEN 1 END) AS TotalChips,\r\n COUNT(CASE WHEN WinningTeam.CoachID = CoachA.CoachID AND Game.[Round] = 4 THEN 1 END) AS FinalFourAppearances \r\n" +
"FROM\r\n NCAA.HeadCoach CoachA\r\n INNER JOIN NCAA.Team WinningTeam ON WinningTeam.CoachID = CoachA.CoachID\r\n INNER JOIN NCAA.College CollegeA ON CollegeA.CollegeID = WinningTeam.CollegeID\r\n INNER JOIN NCAA.Game ON Game.WinningTeamID = WinningTeam.TeamID\r\n INNER JOIN NCAA.HeadCoach WinningCoach ON WinningCoach.CoachID = WinningTeam.CoachID\r\n INNER JOIN NCAA.Team LosingTeam ON Game.LosingTeamID = LosingTeam.TeamID\r\n INNER JOIN NCAA.HeadCoach LosingCoach ON LosingCoach.CoachID = LosingTeam.CoachID\r\n" +
"WHERE\r\n CoachA.FirstName = '{0}'\r\n AND CoachA.LastName = '{1}'\r\n" +
"GROUP BY\r\n CoachA.FirstName,\r\n CoachA.LastName,\r\n CollegeA.[Name];", name1[0], name1[1]);
try
{
SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder();
cb.UserID = "luke3802";
cb.Password = "55Warriors2020";
cb.DataSource = "mssql.cs.ksu.edu";
cb["Database"] = "luke3802";
cb.TrustServerCertificate = true;
SqlDataAdapter adapter = new SqlDataAdapter();
using (SqlConnection connection = new SqlConnection(cb.ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(firstCoachQuery, connection))
{
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
string fullName = reader.GetString(0) + " " + reader.GetString(1);
string collegeName = reader.GetString(2);
int totalWins = reader.GetInt32(3);
int totalLosses = reader.GetInt32(4);
int totalChips = reader.GetInt32(5);
int finalFourAppearances = reader.GetInt32(6);
string output = string.Format("Coach: {0}\nCollege: {1}\nTotal Wins: {2}\nTotal Losses: {3}\nTotal Chips: {4}\nFinal Four Appearances: {5}",
fullName, collegeName, totalWins, totalLosses, totalChips, finalFourAppearances);
coachRichTextBox1.Text = output;
}
}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
else if(index == 2)
{
string coach2 = dropDownList2.SelectedItem.ToString();
string[] name2 = coach2.Split(' ');
string secondCoachQuery = String.Format(
"SELECT\r\n CoachA.FirstName,\r\n CoachA.LastName,\r\n CollegeA.[Name] AS CollegeName,\r\n COUNT(CASE WHEN WinningCoach.CoachID = CoachA.CoachID THEN 1 END) AS TotalWins,\r\n COUNT(CASE WHEN LosingCoach.CoachID = CoachA.CoachID THEN 1 END) AS TotalLosses,\r\n COUNT(CASE WHEN WinningTeam.CoachID = CoachA.CoachID AND Game.[Round] = 2 THEN 1 END) AS TotalChips,\r\n COUNT(CASE WHEN WinningTeam.CoachID = CoachA.CoachID AND Game.[Round] = 4 THEN 1 END) AS FinalFourAppearances \r\n" +
"FROM\r\n NCAA.HeadCoach CoachA\r\n INNER JOIN NCAA.Team WinningTeam ON WinningTeam.CoachID = CoachA.CoachID\r\n INNER JOIN NCAA.College CollegeA ON CollegeA.CollegeID = WinningTeam.CollegeID\r\n INNER JOIN NCAA.Game ON Game.WinningTeamID = WinningTeam.TeamID\r\n INNER JOIN NCAA.HeadCoach WinningCoach ON WinningCoach.CoachID = WinningTeam.CoachID\r\n INNER JOIN NCAA.Team LosingTeam ON Game.LosingTeamID = LosingTeam.TeamID\r\n INNER JOIN NCAA.HeadCoach LosingCoach ON LosingCoach.CoachID = LosingTeam.CoachID\r\n" +
"WHERE\r\n CoachA.FirstName = '{0}'\r\n AND CoachA.LastName = '{1}'\r\n" +
"GROUP BY\r\n CoachA.FirstName,\r\n CoachA.LastName,\r\n CollegeA.[Name];", name2[0], name2[1]);
try
{
SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder();
cb.UserID = "luke3802";
cb.Password = "55Warriors2020";
cb.DataSource = "mssql.cs.ksu.edu";
cb["Database"] = "luke3802";
cb.TrustServerCertificate = true;
SqlDataAdapter adapter = new SqlDataAdapter();
using (SqlConnection connection = new SqlConnection(cb.ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(secondCoachQuery, connection))
{
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
string fullName = reader.GetString(0) + " " + reader.GetString(1);
string collegeName = reader.GetString(2);
int totalWins = reader.GetInt32(3);
int totalLosses = reader.GetInt32(4);
int totalChips = reader.GetInt32(5);
int finalFourAppearances = reader.GetInt32(6);
string output = string.Format("Coach: {0}\nCollege: {1}\nTotal Wins: {2}\nTotal Losses: {3}\nTotal Chips: {4}\nFinal Four Appearances: {5}",
fullName, collegeName, totalWins, totalLosses, totalChips, finalFourAppearances);
coachRichTextBox2.Text = output;
}
}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("HOW?????");
}
}
}
}