Room.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  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. using System.Data.SqlClient;
  18. namespace HotelCalifornia
  19. {
  20. /// <summary>
  21. /// Логика взаимодействия для Room.xaml
  22. /// </summary>
  23. public partial class Room : Window
  24. {
  25. public Room()
  26. {
  27. InitializeComponent();
  28. //Таймер на обновление времени
  29. DispatcherTimer timer = new DispatcherTimer();
  30. timer.Tick += new EventHandler(Update_Timer_Tick);
  31. timer.Interval = new TimeSpan(0, 0, 1);
  32. timer.Start();
  33. }
  34. //Строка подключения
  35. SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=kursah;Integrated Security=True");
  36. //Значение статуса
  37. string status = "";
  38. //Вывод даты и время в textblock
  39. private void Update_Timer_Tick(object sender, EventArgs e)
  40. {
  41. timetxt.Text = DateTime.Now.ToString();
  42. }
  43. //Перетаскивание окна
  44. private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
  45. {
  46. DragMove();
  47. }
  48. //Возврат к окну выбора функции
  49. private void Back(object sender, RoutedEventArgs e)
  50. {
  51. Variant variant = new Variant();
  52. variant.idadmintxt.Text = idadmintxt.Text;
  53. this.Close();
  54. variant.Show();
  55. }
  56. //Выход из приложения
  57. private void Close(object sender, RoutedEventArgs e)
  58. {
  59. Application.Current.Shutdown();
  60. }
  61. //Ограничение для ввода текста
  62. #region Ограничения
  63. //Для ввода только цифр
  64. private void numbertxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
  65. {
  66. e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  67. }
  68. //Для ввода только цифр
  69. private void telephonetxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
  70. {
  71. e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  72. }
  73. //Запрет пробела
  74. private void numbertxt_PreviewKeyDown(object sender, KeyEventArgs e)
  75. {
  76. if (e.Key == Key.Space)
  77. {
  78. e.Handled = true;
  79. }
  80. }
  81. //Запрет пробела
  82. private void telephonetxt_PreviewKeyDown(object sender, KeyEventArgs e)
  83. {
  84. if (e.Key == Key.Space)
  85. {
  86. e.Handled = true;
  87. }
  88. }
  89. #endregion
  90. //Добавление комнаты
  91. private void Add_Click(object sender, RoutedEventArgs e)
  92. {
  93. try
  94. {
  95. if (numbertxt.Text == "" || telephonetxt.Text == "")
  96. {
  97. MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  98. }
  99. else if (numbertxt.Text.Length < 3 || telephonetxt.Text.Length < 9)
  100. {
  101. MessageBox.Show("Поля не полностью заполены!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  102. }
  103. else
  104. {
  105. con.Open();
  106. SqlCommand cmd = new SqlCommand("Select * from Room where Number_Room ='" + numbertxt.Text + "'", con);
  107. cmd.CommandType = CommandType.Text;
  108. SqlDataAdapter adapter = new SqlDataAdapter();
  109. adapter.SelectCommand = cmd;
  110. DataSet dataSet = new DataSet();
  111. adapter.Fill(dataSet);
  112. if (dataSet.Tables[0].Rows.Count > 0)
  113. {
  114. MessageBox.Show("Такая комната уже есть!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
  115. con.Close();
  116. }
  117. else
  118. {
  119. if (yescheck.IsChecked == true)
  120. status = "1";
  121. else
  122. status = "2";
  123. string sql = "INSERT INTO Room (Number_Room,Telephone_Room,Status_Room) VALUES('" + numbertxt.Text + "','" + telephonetxt.Text + "','" + status.ToString() + "')";
  124. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  125. dataAdapter.SelectCommand.ExecuteNonQuery();
  126. con.Close();
  127. idroomtxt.Text = "";
  128. numbertxt.Text = "";
  129. telephonetxt.Text = "";
  130. yescheck.IsChecked = true;
  131. showgrid();
  132. MessageBox.Show("Комната добавлена!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  133. }
  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. try
  146. {
  147. if (idroomtxt.Text == "")
  148. {
  149. MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  150. }
  151. else if (numbertxt.Text == "" || telephonetxt.Text == "")
  152. {
  153. MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  154. }
  155. else if (numbertxt.Text.Length < 3 || telephonetxt.Text.Length < 9)
  156. {
  157. MessageBox.Show("Поля не полностью заполены!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  158. }
  159. else
  160. {
  161. try
  162. {
  163. if (yescheck.IsChecked == true)
  164. status = "1";
  165. else
  166. status = "2";
  167. con.Open();
  168. string sql = "Update Room set Number_Room ='" + numbertxt.Text + "', Telephone_Room = '" + telephonetxt.Text + "', Status_Room = '" + status.ToString() + "' where ID_Room = '" + idroomtxt.Text + "'";
  169. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  170. dataAdapter.SelectCommand.ExecuteNonQuery();
  171. con.Close();
  172. idroomtxt.Text = "";
  173. numbertxt.Text = "";
  174. telephonetxt.Text = "";
  175. yescheck.IsChecked = true;
  176. showgrid();
  177. MessageBox.Show("Комната изменена!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  178. }
  179. catch (Exception ex)
  180. {
  181. con.Close();
  182. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  183. }
  184. }
  185. }
  186. catch (Exception ex)
  187. {
  188. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  189. }
  190. }
  191. //Удаление комнаты
  192. private void Delete_Click(object sender, RoutedEventArgs e)
  193. {
  194. if (idroomtxt.Text == "")
  195. {
  196. MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  197. }
  198. else
  199. {
  200. try
  201. {
  202. con.Open();
  203. string sql = "DELETE FROM Room WHERE ID_Room = '" + idroomtxt.Text + "'";
  204. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  205. dataAdapter.SelectCommand.ExecuteNonQuery();
  206. con.Close();
  207. idroomtxt.Text = "";
  208. numbertxt.Text = "";
  209. telephonetxt.Text = "";
  210. yescheck.IsChecked = true;
  211. showgrid();
  212. MessageBox.Show("Комната удалена!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
  213. }
  214. catch (Exception ex)
  215. {
  216. con.Close();
  217. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  218. }
  219. }
  220. }
  221. //Свернуть окно
  222. private void WindMin_Click(object sender, RoutedEventArgs e)
  223. {
  224. this.WindowState = WindowState.Minimized;
  225. }
  226. //Фомировка данных из БД
  227. void showgrid()
  228. {
  229. try
  230. {
  231. con.Open();
  232. string sql = "SELECT ID_Room, Number_Room, Telephone_Room, RoomStatus.Status From Room inner join RoomStatus on RoomStatus.ID_Status = Room.Status_Room";
  233. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  234. DataTable data = new DataTable("Room");
  235. dataAdapter.Fill(data);
  236. dataroom.ItemsSource = data.DefaultView;
  237. dataAdapter.Update(data);
  238. con.Close();
  239. dataroom.Columns[0].Header = "ID";
  240. dataroom.Columns[1].Header = "Номер комнты";
  241. dataroom.Columns[2].Header = "Телефон комнаты";
  242. dataroom.Columns[3].Header = "Статус комнаты";
  243. dataroom.Columns[0].Visibility = Visibility.Collapsed;
  244. }
  245. catch (Exception ex)
  246. {
  247. con.Close();
  248. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  249. }
  250. }
  251. //Выбор строки из БД
  252. private void dataroom_SelectionChanged(object sender, SelectionChangedEventArgs e)
  253. {
  254. try
  255. {
  256. DataGrid gd = (DataGrid)sender;
  257. DataRowView rowView = gd.SelectedItem as DataRowView;
  258. if (rowView != null)
  259. {
  260. idroomtxt.Text = rowView["ID_Room"].ToString();
  261. numbertxt.Text = rowView["Number_Room"].ToString();
  262. telephonetxt.Text = rowView["Telephone_Room"].ToString();
  263. status = rowView["Status"].ToString();
  264. if (status == "Свободна")
  265. yescheck.IsChecked = true;
  266. else
  267. nocheck.IsChecked = true;
  268. }
  269. }
  270. catch (Exception ex)
  271. {
  272. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  273. }
  274. }
  275. //Обновление
  276. private void Refresh_Click(object sender, RoutedEventArgs e)
  277. {
  278. showgrid();
  279. idroomtxt.Text = "";
  280. numbertxt.Text = "";
  281. telephonetxt.Text = "";
  282. yescheck.IsChecked = true;
  283. }
  284. //Запуск в самом начале
  285. private void Window_Loaded(object sender, RoutedEventArgs e)
  286. {
  287. showgrid();
  288. }
  289. }
  290. }