Client.xaml.cs 12 KB

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