Client.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Shapes;
  15. using System.Windows.Threading;
  16. using System.Data;
  17. namespace HotelCalifornia
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для Client.xaml
  21. /// </summary>
  22. public partial class Client : Window
  23. {
  24. public Client()
  25. {
  26. InitializeComponent();
  27. //Таймер на обновление времени
  28. DispatcherTimer timer = new DispatcherTimer();
  29. timer.Tick += new EventHandler(Update_Timer_Tick);
  30. timer.Interval = new TimeSpan(0, 0, 1);
  31. timer.Start();
  32. }
  33. //Строка подключения
  34. SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=kursah;Integrated Security=True");
  35. //Вывод даты и время в textblock
  36. private void Update_Timer_Tick(object sender, EventArgs e)
  37. {
  38. timetxt.Text = DateTime.Now.ToString();
  39. }
  40. //Перетаскивание окна
  41. private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
  42. {
  43. try
  44. {
  45. DragMove();
  46. }
  47. catch
  48. {
  49. }
  50. }
  51. //Возврат к окну выбора функции
  52. private void Back(object sender, RoutedEventArgs e)
  53. {
  54. Variant variant = new Variant();
  55. variant.idadmintxt.Text = idadmintxt.Text;
  56. this.Close();
  57. variant.Show();
  58. }
  59. //Выход из приложения
  60. private void Close(object sender, RoutedEventArgs e)
  61. {
  62. Application.Current.Shutdown();
  63. }
  64. //Свернуть окно
  65. private void WindMin_Click(object sender, RoutedEventArgs e)
  66. {
  67. this.WindowState = WindowState.Minimized;
  68. }
  69. //Ограничение для ввода текста
  70. #region Ограничение
  71. private void familiatxt_TextChanged(object sender, TextChangedEventArgs e)
  72. {
  73. if (sender is TextBox textBox)
  74. {
  75. textBox.Text = new string
  76. (textBox.Text.Where(ch => (ch >= 'А' && ch <= 'Я') || (ch >= 'а' && ch <= 'я')).ToArray());
  77. }
  78. }
  79. private void nametxt_TextChanged(object sender, TextChangedEventArgs e)
  80. {
  81. if (sender is TextBox textBox)
  82. {
  83. textBox.Text = new string
  84. (textBox.Text.Where(ch => (ch >= 'А' && ch <= 'Я') || (ch >= 'а' && ch <= 'я')).ToArray());
  85. }
  86. }
  87. private void otchestvotxt_TextChanged(object sender, TextChangedEventArgs e)
  88. {
  89. if (sender is TextBox textBox)
  90. {
  91. textBox.Text = new string
  92. (textBox.Text.Where(ch => (ch >= 'А' && ch <= 'Я') || (ch >= 'а' && ch <= 'я')).ToArray());
  93. }
  94. }
  95. private void telephonetxt_TextChanged(object sender, TextChangedEventArgs e)
  96. {
  97. if (sender is TextBox textBox)
  98. {
  99. textBox.Text = new string
  100. (textBox.Text.Where(ch => (ch >= '0' && ch <= '9')).ToArray());
  101. }
  102. }
  103. private void passporttxt_TextChanged(object sender, TextChangedEventArgs e)
  104. {
  105. if (sender is TextBox textBox)
  106. {
  107. textBox.Text = new string
  108. (textBox.Text.Where(ch => (ch >= '0' && ch <= '9')).ToArray());
  109. }
  110. }
  111. #endregion
  112. //Добавление клиента
  113. private void Add_Click(object sender, RoutedEventArgs e)
  114. {
  115. try
  116. {
  117. if (nametxt.Text == "" || familiatxt.Text=="" || telephonetxt.Text == "" || otchestvotxt.Text == "" || passporttxt.Text == "")
  118. {
  119. MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  120. }
  121. else if (passporttxt.Text.Length < 10 || telephonetxt.Text.Length < 11)
  122. {
  123. MessageBox.Show("Поле номер телефона и паспорт не полностью заполнены!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  124. }
  125. else
  126. {
  127. con.Open();
  128. 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 + "')";
  129. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  130. dataAdapter.SelectCommand.ExecuteNonQuery();
  131. con.Close();
  132. idclienttxt.Text = "";
  133. nametxt.Text = "";
  134. familiatxt.Text = "";
  135. otchestvotxt.Text = "";
  136. telephonetxt.Text = "";
  137. passporttxt.Text = "";
  138. showgrid();
  139. MessageBox.Show("Клиент добавлен!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  140. }
  141. }
  142. catch (Exception ex)
  143. {
  144. con.Close();
  145. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  146. }
  147. }
  148. //Обновление клиента
  149. private void Update_Click(object sender, RoutedEventArgs e)
  150. {
  151. if (idclienttxt.Text == "")
  152. {
  153. MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  154. }
  155. else if (familiatxt.Text == "" || nametxt.Text == "" || otchestvotxt.Text == "" || telephonetxt.Text == "" || passporttxt.Text == "")
  156. {
  157. MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  158. }
  159. else if (passporttxt.Text.Length < 10 || telephonetxt.Text.Length < 11)
  160. {
  161. MessageBox.Show("Поля не полностью заполены!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  162. }
  163. else
  164. {
  165. try
  166. {
  167. con.Open();
  168. 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 + "'";
  169. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  170. dataAdapter.SelectCommand.ExecuteNonQuery();
  171. con.Close();
  172. idclienttxt.Text = "";
  173. nametxt.Text = "";
  174. familiatxt.Text = "";
  175. otchestvotxt.Text = "";
  176. telephonetxt.Text = "";
  177. passporttxt.Text = "";
  178. showgrid();
  179. MessageBox.Show("Клиент изменен!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
  180. }
  181. catch (Exception ex)
  182. {
  183. con.Close();
  184. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  185. }
  186. }
  187. }
  188. //Удаление клиента
  189. private void Delete_Click(object sender, RoutedEventArgs e)
  190. {
  191. if (idclienttxt.Text == "")
  192. {
  193. MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  194. }
  195. else
  196. {
  197. try
  198. {
  199. con.Open();
  200. string sql = "DELETE FROM Client WHERE ID_Client = '" + idclienttxt.Text + "'";
  201. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  202. dataAdapter.SelectCommand.ExecuteNonQuery();
  203. con.Close();
  204. idclienttxt.Text = "";
  205. nametxt.Text = "";
  206. familiatxt.Text = "";
  207. otchestvotxt.Text = "";
  208. telephonetxt.Text = "";
  209. passporttxt.Text = "";
  210. showgrid();
  211. MessageBox.Show("Клиент удален!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
  212. }
  213. catch (Exception ex)
  214. {
  215. con.Close();
  216. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  217. }
  218. }
  219. }
  220. //Фомировка данных из БД
  221. void showgrid()
  222. {
  223. try
  224. {
  225. con.Open();
  226. string sql = "SELECT ID_Client, LastName_Client, FirstName_Client, MiddleName_Client, Telephone_Client, Passport From Client";
  227. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  228. DataTable data = new DataTable("Client");
  229. dataAdapter.Fill(data);
  230. dataclient.ItemsSource = data.DefaultView;
  231. dataAdapter.Update(data);
  232. con.Close();
  233. dataclient.Columns[0].Header = "ID";
  234. dataclient.Columns[1].Header = "Фамилия";
  235. dataclient.Columns[2].Header = "Имя";
  236. dataclient.Columns[3].Header = "Отчество";
  237. dataclient.Columns[4].Header = "Телефон";
  238. dataclient.Columns[5].Header = "Паспорт";
  239. dataclient.Columns[0].Visibility = Visibility.Collapsed;
  240. }
  241. catch (Exception ex)
  242. {
  243. con.Close();
  244. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  245. }
  246. }
  247. //Выбор строки из БД
  248. private void dataclient_SelectionChanged(object sender, SelectionChangedEventArgs e)
  249. {
  250. try
  251. {
  252. DataGrid gd = (DataGrid)sender;
  253. DataRowView rowView = gd.SelectedItem as DataRowView;
  254. if (rowView != null)
  255. {
  256. idclienttxt.Text = rowView["ID_Client"].ToString();
  257. familiatxt.Text = rowView["LastName_Client"].ToString();
  258. nametxt.Text = rowView["FirstName_Client"].ToString();
  259. otchestvotxt.Text = rowView["MiddleName_Client"].ToString();
  260. telephonetxt.Text = rowView["Telephone_Client"].ToString();
  261. passporttxt.Text = rowView["Passport"].ToString();
  262. }
  263. }
  264. catch (Exception ex)
  265. {
  266. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  267. }
  268. }
  269. //Обновление
  270. private void Refresh_Click(object sender, RoutedEventArgs e)
  271. {
  272. idclienttxt.Text = "";
  273. nametxt.Text = "";
  274. familiatxt.Text = "";
  275. otchestvotxt.Text = "";
  276. telephonetxt.Text = "";
  277. passporttxt.Text = "";
  278. showgrid();
  279. }
  280. //Запуск в самом начале
  281. private void Window_Loaded(object sender, RoutedEventArgs e)
  282. {
  283. showgrid();
  284. }
  285. }
  286. }