123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- using System;
- using System.Collections.Generic;
- 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;
- using System.Windows.Threading;
- using System.Data;
- namespace HotelCalifornia
- {
- /// <summary>
- /// Логика взаимодействия для Client.xaml
- /// </summary>
- public partial class Client : Window
- {
- public Client()
- {
- 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");
- //Вывод даты и время в textblock
- private void Update_Timer_Tick(object sender, EventArgs e)
- {
- timetxt.Text = DateTime.Now.ToString();
- }
- //Перетаскивание окна
- private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
- {
- try
- {
- DragMove();
- }
- catch
- {
- }
- }
- //Возврат к окну выбора функции
- private void Back(object sender, RoutedEventArgs e)
- {
- Variant variant = new Variant();
- variant.idadmintxt.Text = idadmintxt.Text;
- this.Close();
- variant.Show();
- }
- //Выход из приложения
- private void Close(object sender, RoutedEventArgs e)
- {
- Application.Current.Shutdown();
- }
- //Свернуть окно
- private void WindMin_Click(object sender, RoutedEventArgs e)
- {
- this.WindowState = WindowState.Minimized;
- }
- //Ограничение для ввода текста
- #region Ограничение
- private void familiatxt_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 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 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 telephonetxt_TextChanged(object sender, TextChangedEventArgs e)
- {
- if (sender is TextBox textBox)
- {
- textBox.Text = new string
- (textBox.Text.Where(ch => (ch >= '0' && ch <= '9')).ToArray());
- }
- }
- private void passporttxt_TextChanged(object sender, TextChangedEventArgs e)
- {
- if (sender is TextBox textBox)
- {
- textBox.Text = new string
- (textBox.Text.Where(ch => (ch >= '0' && ch <= '9')).ToArray());
- }
- }
- #endregion
- //Добавление клиента
- private void Add_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (nametxt.Text == "" || familiatxt.Text=="" || telephonetxt.Text == "" || otchestvotxt.Text == "" || passporttxt.Text == "")
- {
- MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- else if (passporttxt.Text.Length < 10 || telephonetxt.Text.Length < 11)
- {
- MessageBox.Show("Поле номер телефона и паспорт не полностью заполнены!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- else
- {
- con.Open();
- string sql = "INSERT INTO Client (LastName_Client,FirstName_Client,MiddleName_Client,Telephone_Client,Passport) VALUES('" + familiatxt.Text + "','" + nametxt.Text + "','" + otchestvotxt.Text + "','" + telephonetxt.Text + "','" + passporttxt.Text + "')";
- SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
- dataAdapter.SelectCommand.ExecuteNonQuery();
- con.Close();
- idclienttxt.Text = "";
- nametxt.Text = "";
- familiatxt.Text = "";
- otchestvotxt.Text = "";
- telephonetxt.Text = "";
- passporttxt.Text = "";
- showgrid();
- 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 (idclienttxt.Text == "")
- {
- MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- else if (familiatxt.Text == "" || nametxt.Text == "" || otchestvotxt.Text == "" || telephonetxt.Text == "" || passporttxt.Text == "")
- {
- MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- else if (passporttxt.Text.Length < 10 || telephonetxt.Text.Length < 11)
- {
- MessageBox.Show("Поля не полностью заполены!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- else
- {
- try
- {
- con.Open();
- string sql = "Update Client set LastName_Client ='" + familiatxt.Text + "', FirstName_Client = '" + nametxt.Text + "', MiddleName_Client = '" + otchestvotxt.Text + "', Telephone_Client = '" + telephonetxt.Text + "', Passport = '" + passporttxt.Text + "' where ID_Client = '" + idclienttxt.Text + "'";
- SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
- dataAdapter.SelectCommand.ExecuteNonQuery();
- con.Close();
- idclienttxt.Text = "";
- nametxt.Text = "";
- familiatxt.Text = "";
- otchestvotxt.Text = "";
- telephonetxt.Text = "";
- passporttxt.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 (idclienttxt.Text == "")
- {
- MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- else
- {
- try
- {
- con.Open();
- string sql = "DELETE FROM Client WHERE ID_Client = '" + idclienttxt.Text + "'";
- SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
- dataAdapter.SelectCommand.ExecuteNonQuery();
- con.Close();
- idclienttxt.Text = "";
- nametxt.Text = "";
- familiatxt.Text = "";
- otchestvotxt.Text = "";
- telephonetxt.Text = "";
- passporttxt.Text = "";
- showgrid();
- MessageBox.Show("Клиент удален!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- catch (Exception ex)
- {
- con.Close();
- MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- }
- //Фомировка данных из БД
- void showgrid()
- {
- try
- {
- con.Open();
- string sql = "SELECT ID_Client, LastName_Client, FirstName_Client, MiddleName_Client, Telephone_Client, Passport From Client";
- SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
- DataTable data = new DataTable("Client");
- dataAdapter.Fill(data);
- dataclient.ItemsSource = data.DefaultView;
- dataAdapter.Update(data);
- con.Close();
- dataclient.Columns[0].Header = "ID";
- dataclient.Columns[1].Header = "Фамилия";
- dataclient.Columns[2].Header = "Имя";
- dataclient.Columns[3].Header = "Отчество";
- dataclient.Columns[4].Header = "Телефон";
- dataclient.Columns[5].Header = "Паспорт";
- dataclient.Columns[0].Visibility = Visibility.Collapsed;
- }
- catch (Exception ex)
- {
- con.Close();
- MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- //Выбор строки из БД
- private void dataclient_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- try
- {
- DataGrid gd = (DataGrid)sender;
- DataRowView rowView = gd.SelectedItem as DataRowView;
- if (rowView != null)
- {
- idclienttxt.Text = rowView["ID_Client"].ToString();
- familiatxt.Text = rowView["LastName_Client"].ToString();
- nametxt.Text = rowView["FirstName_Client"].ToString();
- otchestvotxt.Text = rowView["MiddleName_Client"].ToString();
- telephonetxt.Text = rowView["Telephone_Client"].ToString();
- passporttxt.Text = rowView["Passport"].ToString();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- //Обновление
- private void Refresh_Click(object sender, RoutedEventArgs e)
- {
- idclienttxt.Text = "";
- nametxt.Text = "";
- familiatxt.Text = "";
- otchestvotxt.Text = "";
- telephonetxt.Text = "";
- passporttxt.Text = "";
- showgrid();
- }
- //Запуск в самом начале
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- showgrid();
- }
- }
- }
|