using System; using System.Collections.Generic; 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; using System.Windows.Threading; using System.Data.SqlClient; using System.Data; namespace HotelCalifornia { /// /// Логика взаимодействия для Staff.xaml /// public partial class Staff : Window { public Staff() { InitializeComponent(); DispatcherTimer timer = new DispatcherTimer(); timer.Tick += new EventHandler(Update_Timer_Tick); timer.Interval = new TimeSpan(0, 0, 1); timer.Start(); } SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=kursah;Integrated Security=True"); private void Update_Timer_Tick(object sender, EventArgs e) { timetxt.Text = DateTime.Now.ToString(); } private void Close(object sender, RoutedEventArgs e) { MessageBoxResult result = MessageBox.Show("Вы хотите выйти из приложения?", "Предупреждение", MessageBoxButton.YesNo, MessageBoxImage.Question); switch (result) { case MessageBoxResult.Yes: Application.Current.Shutdown(); break; case MessageBoxResult.No: break; } } private void WindMin_Click(object sender, RoutedEventArgs e) { this.WindowState = WindowState.Minimized; } private void Back(object sender, RoutedEventArgs e) { MessageBoxResult result = MessageBox.Show("Вы хотите вернуться к предыдущему окну?", "Предупреждение", MessageBoxButton.YesNo, MessageBoxImage.Question); switch (result) { case MessageBoxResult.Yes: Variant variant = new Variant(); this.Close(); variant.Show(); break; case MessageBoxResult.No: break; } } private void datastaff_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { DataGrid gd = (DataGrid)sender; DataRowView rowView = gd.SelectedItem as DataRowView; if (rowView != null) { idtxt.Text = rowView["ID_Administrator"].ToString(); nametxt.Text = rowView["FirstName"].ToString(); familiyatxt.Text = rowView["LastName"].ToString(); otchestvotxt.Text = rowView["MiddleName"].ToString(); logintxt.Text = rowView["Login"].ToString(); passwordtxt.Text = rowView["Password"].ToString(); } } catch (Exception ex) { MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } private void Add_Click(object sender, RoutedEventArgs e) { if (familiyatxt.Text == "" || nametxt.Text == "" || otchestvotxt.Text == "" || logintxt.Text == "" || passwordtxt.Text == "") { MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information); } else { try { con.Open(); SqlCommand cmd = new SqlCommand("Select * from Administrator where Login ='" + logintxt.Text + "'", con); cmd.CommandType = CommandType.Text; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = cmd; DataSet dataSet = new DataSet(); adapter.Fill(dataSet); if (dataSet.Tables[0].Rows.Count > 0) { MessageBox.Show("Такой Администратор уже создан!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information); con.Close(); } else { string reg = "INSERT INTO Administrator (LastName,FirstName,MiddleName,Login,Password) VALUES('" + familiyatxt.Text + "','" + nametxt.Text + "','" + otchestvotxt.Text + "','" + logintxt.Text + "','" + passwordtxt.Text + "')"; SqlDataAdapter dataAdapter = new SqlDataAdapter(reg, con); dataAdapter.SelectCommand.ExecuteNonQuery(); con.Close(); showgrid(); idtxt.Text = ""; nametxt.Text = ""; familiyatxt.Text = ""; otchestvotxt.Text = ""; logintxt.Text = ""; passwordtxt.Text = ""; MessageBox.Show("Администратор был добавлен!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information); } } catch (Exception ex) { con.Close(); MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } } private void Update_Click(object sender, RoutedEventArgs e) { if (idtxt.Text == "") { MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information); } else if (familiyatxt.Text == "" || nametxt.Text == "" || otchestvotxt.Text == "" || logintxt.Text == "" || passwordtxt.Text == "") { MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information); } else { try { con.Open(); string sql = "Update Administrator set FirstName ='" + nametxt.Text + "', LastName = '" + familiyatxt.Text + "', MiddleName = '" + otchestvotxt.Text + "', Login = '" + logintxt.Text + "', Password = '" + passwordtxt.Text + "' where ID_Administrator = '" + idtxt.Text + "'"; SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con); dataAdapter.SelectCommand.ExecuteNonQuery(); con.Close(); idtxt.Text = ""; nametxt.Text = ""; familiyatxt.Text = ""; otchestvotxt.Text = ""; logintxt.Text = ""; passwordtxt.Text = ""; showgrid(); MessageBox.Show("Администратор был изменен!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { con.Close(); MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } } private void Delete_Click(object sender, RoutedEventArgs e) { if (idtxt.Text == "") { MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information); } else { try { con.Open(); string sql = "DELETE FROM Administrator WHERE ID_Administrator = '" + idtxt.Text + "'"; SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con); dataAdapter.SelectCommand.ExecuteNonQuery(); con.Close(); idtxt.Text = ""; nametxt.Text = ""; familiyatxt.Text = ""; otchestvotxt.Text = ""; logintxt.Text = ""; passwordtxt.Text = ""; showgrid(); MessageBox.Show("Администратор удален!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { con.Close(); MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } } private void nametxt_TextChanged(object sender, TextChangedEventArgs e) { if (sender is TextBox textBox) { textBox.Text = new string (textBox.Text.Where(ch => (ch >= 'А' && ch <= 'Я') || (ch >= 'а' && ch <= 'я')).ToArray()); } } private void familiyatxt_TextChanged(object sender, TextChangedEventArgs e) { if (sender is TextBox textBox) { textBox.Text = new string (textBox.Text.Where(ch => (ch >= 'А' && ch <= 'Я') || (ch >= 'а' && ch <= 'я')).ToArray()); } } private void otchestvotxt_TextChanged(object sender, TextChangedEventArgs e) { if (sender is TextBox textBox) { textBox.Text = new string (textBox.Text.Where(ch => (ch >= 'А' && ch <= 'Я') || (ch >= 'а' && ch <= 'я')).ToArray()); } } private void logintxt_TextChanged(object sender, TextChangedEventArgs e) { if (sender is TextBox textBox) { textBox.Text = new string (textBox.Text.Where(ch => (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')).ToArray()); } } private void passwordtxt_TextChanged(object sender, TextChangedEventArgs e) { if (sender is TextBox textBox) { textBox.Text = new string (textBox.Text.Where(ch => (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')).ToArray()); } } private void Grid_Loaded(object sender, RoutedEventArgs e) { showgrid(); } void showgrid() { try { con.Open(); string rke = "SELECT * From Administrator"; SqlDataAdapter dataAdapter = new SqlDataAdapter(rke, con); DataTable data = new DataTable("Administrator"); dataAdapter.Fill(data); datastaff.ItemsSource = data.DefaultView; dataAdapter.Update(data); con.Close(); datastaff.Columns[0].Header = "ID"; datastaff.Columns[1].Header = "Фамилия"; datastaff.Columns[2].Header = "Имя"; datastaff.Columns[3].Header = "Отчество"; datastaff.Columns[4].Header = "Логин"; datastaff.Columns[5].Header = "Пароль"; datastaff.Columns[0].Visibility = Visibility.Collapsed; } catch (Exception ex) { MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } private void Refresh_Click(object sender, RoutedEventArgs e) { showgrid(); idtxt.Text = ""; nametxt.Text = ""; familiyatxt.Text = ""; otchestvotxt.Text = ""; logintxt.Text = ""; passwordtxt.Text = ""; } } }