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 { /// /// Логика взаимодействия для AddUchenik.xaml /// public partial class AddUchenik : Window { public AddUchenik() { 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 (familiyauch.Text != "" && imyauch.Text != "" && otchuch.Text != "" && datauch.Text != "" && numberuch.Text != "" && disciplina.Text != "" && pol_id.Text != "" && gruppa.Text != "") { bool fam = false, imya = false, otch = false, data = false, num = false; bool dis = false, pol = false, gr = false; for (int i = 0; i < familiyauch.Text.Length; i++) { if (fam) { fam = false; } if (familiyauch.Text[i] >= 'А' && familiyauch.Text[i] <= 'Я' || familiyauch.Text[i] >= 'а' && familiyauch.Text[i] <= 'я') { fam = true; break; } if (fam == false) { MessageBox.Show("Поле фамилия может содержать только русские символы"); break; } } for (int i = 0; i < imyauch.Text.Length; i++) { if (imya) { imya = false; } if (imyauch.Text[i] >= 'А' && imyauch.Text[i] <= 'Я' || imyauch.Text[i] >= 'а' && imyauch.Text[i] <= 'я') { imya = true; break; } if (imya == false) { MessageBox.Show("Поле имя может содержать только русские символы"); break; } } for (int i = 0; i < otchuch.Text.Length; i++) { if (otch) { otch = false; } if (otchuch.Text[i] >= 'А' && otchuch.Text[i] <= 'Я' || otchuch.Text[i] >= 'а' && otchuch.Text[i] <= 'я') { otch = true; break; } if (otch == false) { MessageBox.Show("Поле отчество может содержать только русские символы"); break; } } for (int i = 0; i < datauch.Text.Length; i++) { if (data) { data = false; } if(datauch.Text.Length > 0) { data = true; break; } if (data == false) { MessageBox.Show("Поле дата рождения может содержать только цифры"); break; } } for (int i = 0; i < numberuch.Text.Length; i++)//проверка номера телефона { if (num) { num = false; } if (numberuch.Text[i] >= '0' && numberuch.Text[i] <= '9' && numberuch.Text.Length == 11) { num = true; break; } if (num == false) { MessageBox.Show("Поле номер телефона может содержать только цифры (без знака + и 11 цифр)"); break; } } DataTable sel = Select("select * from [dbo].[disciplina] where id = " + disciplina.Text); for (int i = 0; i < disciplina.Text.Length; i++) { if (dis) { dis = false; } if (disciplina.Text[i] >= '0' && disciplina.Text[i] <= '7' && disciplina.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].[pol] where id = " + pol_id.Text); for (int i = 0; i < pol_id.Text.Length; i++) { if (pol) { pol = false; } if (pol_id.Text[i] == '1' || pol_id.Text[i] == '2' && pol_id.Text.Length == 1) { if (sel2.Rows.Count > 0) { pol = true; } else { pol = false; } } if (pol == false) { MessageBox.Show("Поле пол должно содержать только цифры 1 (мужской) и 2 (женский)"); break; } } if(fam && imya && otch && data && num && dis && pol && gr) { DataTable dt_infouchenik = Insert("insert into [dbo].[infouchenik] values " + "('" + familiyauch.Text + "','" + imyauch.Text + "','" + otchuch.Text + "','" + datauch.Text + "','" + disciplina.Text + "','" + gruppa.Text + "','" + pol_id.Text + "','" + numberuch.Text + "')"); MessageBox.Show("Ученик добавлен"); SpisokUchenikov mainreg = new SpisokUchenikov(); mainreg.Show(); Close(); } else { MessageBox.Show("Неправильный формат"); } } else { MessageBox.Show("Все строки должны быть заполнены"); } } private void Button_Click_Back(object sender, RoutedEventArgs e) { SpisokUchenikov spisok = new SpisokUchenikov(); spisok.Show(); Close(); } 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; } } }