using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace KursachMukha692gr { /// /// Логика взаимодействия для AddTournment.xaml /// public partial class AddTournment : Window { public AddTournment() { InitializeComponent(); } public DataTable Select(string selectSQL) { DataTable dataTable = new DataTable("dataBase"); SqlConnection sqlConnection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;"); sqlConnection.Open(); SqlCommand sqlCommand = sqlConnection.CreateCommand(); sqlCommand.CommandText = selectSQL; SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand); sqlDataAdapter.Fill(dataTable); sqlConnection.Close(); return dataTable; } public DataTable Select1(string selectSQL) { DataTable dataTable = new DataTable("dataBase"); SqlConnection sqlConnection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;"); sqlConnection.Open(); SqlCommand sqlCommand = sqlConnection.CreateCommand(); sqlCommand.CommandText = selectSQL; SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand); sqlDataAdapter.Fill(dataTable); sqlConnection.Close(); return dataTable; } public DataTable Select2(string selectSQL) { DataTable dataTable = new DataTable("dataBase"); SqlConnection sqlConnection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;"); sqlConnection.Open(); SqlCommand sqlCommand = sqlConnection.CreateCommand(); sqlCommand.CommandText = selectSQL; SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand); sqlDataAdapter.Fill(dataTable); sqlConnection.Close(); return dataTable; } private void Button_Click_Add(object sender, RoutedEventArgs e) { if(namesorevi.Text != "" && mestoprovedeniya.Text != "" && disciplina_id.Text != "" && gruppa.Text != "" && infotrener_id.Text != "") { bool name = false, mesto = false, dis = false, gr = false, trener = false; for (int i = 0; i < namesorevi.Text.Length; i++) { if (name) { name = false; } if (namesorevi.Text[i] >='А' && namesorevi.Text[i] <= 'Я' || namesorevi.Text[i] >= 'а' && namesorevi.Text[i] <= 'я' || namesorevi.Text[i] >= '"' || namesorevi.Text[i] <= ',') { name = true; break; } if (name == false) { MessageBox.Show("Поле название турнира может содержать только русские символы (и ковычки и , )"); break; } } for (int i = 0; i < mestoprovedeniya.Text.Length; i++) { if (mesto) { mesto = false; } if (mestoprovedeniya.Text[i] >= 'А' && mestoprovedeniya.Text[i] <= 'Я' || mestoprovedeniya.Text[i] >= 'а' && mestoprovedeniya.Text[i] <= 'я' || mestoprovedeniya.Text[i] >= '"' || mestoprovedeniya.Text[i] <= ',') { mesto = true; break; } if (mesto == false) { MessageBox.Show("Поле место проведения может содержать только русские символы (и ковычки и , )"); break; } } DataTable sel = Select("select * from [dbo].[disciplina] where id = " + disciplina_id.Text); for (int i = 0; i < disciplina_id.Text.Length; i++) { if (dis) { dis = false; } if (disciplina_id.Text[i] >= '0' && disciplina_id.Text[i] <= '7' && disciplina_id.Text.Length == 1) { if (sel.Rows.Count > 0) { dis = true; } else { dis = false; } } if (dis == false) { MessageBox.Show("Поле дисциплина должно содержать код дисциплины (1-хоккей,2-Мини-футбол,3-Волейбол,4-Футбол,5-Баскетбол,6-Плавание,7-Бокс)"); break; } } DataTable sel1 = Select1("select * from [dbo].[gruppa] where id = " + gruppa.Text); for (int i = 0; i < gruppa.Text.Length; i++) { if (gr) { gr = false; } if (gruppa.Text[i] >= '0' && gruppa.Text[i] <= '9' && gruppa.Text.Length == 3) { if (sel1.Rows.Count > 0) { gr = true; } else { gr = false; } } if (gr == false) { MessageBox.Show("Поле группа должно содержать цифры (3 цифры) или вы ввели группу, которой нет"); break; } } DataTable sel2 = Select2("select * from [dbo].[infotrener] where id = " + infotrener_id.Text); for (int i = 0; i < infotrener_id.Text.Length; i++) { if (trener) { trener = false; } if (infotrener_id.Text[i] >= '0' && infotrener_id.Text[i] <= '9') { if (sel2.Rows.Count > 0) { trener = true; } else { trener = false; } } if (trener == false) { MessageBox.Show("Поле тренер должно содержать код тренера или вы ввели код, которого нет"); break; } } if (mesto && dis && trener && gr && name) { DataTable dt_infotrener = Insert("insert into [dbo].[sorevnovaniya] values " + "('" + namesorevi.Text + "','" + mestoprovedeniya.Text + "','" + disciplina_id.Text + "','" + gruppa.Text + "','" + infotrener_id.Text + "')"); MessageBox.Show("Соревнование добавлено"); Tournments mainreg = new Tournments(); mainreg.Show(); Close(); } else { MessageBox.Show("Неправильный формат"); } } else { MessageBox.Show("Все поля должны быть заполнены!"); } } public DataTable Insert(string selectSQL) { DataTable dataTable = new DataTable("dataBase"); SqlConnection sqlConnection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;"); sqlConnection.Open(); SqlCommand sqlCommand = sqlConnection.CreateCommand(); sqlCommand.CommandText = selectSQL; SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand); sqlDataAdapter.Fill(dataTable); sqlConnection.Close(); return dataTable; } private void Button_Click_Back(object sender, RoutedEventArgs e) { Tournments tournments = new Tournments(); tournments.Show(); Close(); } } }