Room.xaml.cs 13 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. 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. string status = "";
  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. MessageBoxResult result = MessageBox.Show("Вы хотите вернуться к предыдущему окну?", "Предупреждение", MessageBoxButton.YesNo, MessageBoxImage.Question);
  49. switch (result)
  50. {
  51. case MessageBoxResult.Yes:
  52. Variant variant = new Variant();
  53. this.Close();
  54. variant.Show();
  55. break;
  56. case MessageBoxResult.No:
  57. break;
  58. }
  59. }
  60. //Закрытие окна
  61. private void Close(object sender, RoutedEventArgs e)
  62. {
  63. MessageBoxResult result = MessageBox.Show("Вы хотите выйти из приложения?", "Предупреждение", MessageBoxButton.YesNo, MessageBoxImage.Question);
  64. switch (result)
  65. {
  66. case MessageBoxResult.Yes:
  67. Application.Current.Shutdown();
  68. break;
  69. case MessageBoxResult.No:
  70. break;
  71. }
  72. }
  73. //Для ввода только цифр
  74. private void numbertxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
  75. {
  76. e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  77. }
  78. //Для ввода только цифр
  79. private void telephonetxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
  80. {
  81. e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  82. }
  83. //Запрет пробела
  84. private void numbertxt_PreviewKeyDown(object sender, KeyEventArgs e)
  85. {
  86. if (e.Key == Key.Space)
  87. {
  88. e.Handled = true;
  89. }
  90. }
  91. //Запрет пробела
  92. private void telephonetxt_PreviewKeyDown(object sender, KeyEventArgs e)
  93. {
  94. if (e.Key == Key.Space)
  95. {
  96. e.Handled = true;
  97. }
  98. }
  99. //Добавление
  100. private void Add_Click(object sender, RoutedEventArgs e)
  101. {
  102. try
  103. {
  104. if (numbertxt.Text == "" || telephonetxt.Text == "")
  105. {
  106. MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  107. }
  108. else if (numbertxt.Text.Length < 3 || telephonetxt.Text.Length < 9)
  109. {
  110. MessageBox.Show("Поля не полностью заполены!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  111. }
  112. else
  113. {
  114. con.Open();
  115. SqlCommand cmd = new SqlCommand("Select * from Room where Number_Room ='" + numbertxt.Text + "'", con);
  116. cmd.CommandType = CommandType.Text;
  117. SqlDataAdapter adapter = new SqlDataAdapter();
  118. adapter.SelectCommand = cmd;
  119. DataSet dataSet = new DataSet();
  120. adapter.Fill(dataSet);
  121. if (dataSet.Tables[0].Rows.Count > 0)
  122. {
  123. MessageBox.Show("Такая комната уже есть!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
  124. con.Close();
  125. }
  126. else
  127. {
  128. if (yescheck.IsChecked == true)
  129. status = "1";
  130. else
  131. status = "2";
  132. string reg = "INSERT INTO Room (Number_Room,Telephone_Room,Status_Room) VALUES('" + numbertxt.Text + "','" + telephonetxt.Text + "','" + status.ToString() + "')";
  133. SqlDataAdapter dataAdapter = new SqlDataAdapter(reg, con);
  134. dataAdapter.SelectCommand.ExecuteNonQuery();
  135. con.Close();
  136. idroomtxt.Text = "";
  137. numbertxt.Text = "";
  138. telephonetxt.Text = "";
  139. yescheck.IsChecked = true;
  140. showgrid();
  141. MessageBox.Show("Комната добавлена!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  142. }
  143. }
  144. }
  145. catch (Exception ex)
  146. {
  147. con.Close();
  148. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  149. }
  150. }
  151. private void WindMin_Click(object sender, RoutedEventArgs e)
  152. {
  153. this.WindowState = WindowState.Minimized;
  154. }
  155. private void Update_Click(object sender, RoutedEventArgs e)
  156. {
  157. try
  158. {
  159. if (idroomtxt.Text == "")
  160. {
  161. MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  162. }
  163. else if (numbertxt.Text == "" || telephonetxt.Text == "")
  164. {
  165. MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  166. }
  167. else if (numbertxt.Text.Length < 3 || telephonetxt.Text.Length < 9)
  168. {
  169. MessageBox.Show("Поля не полностью заполены!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  170. }
  171. else
  172. {
  173. try
  174. {
  175. if (yescheck.IsChecked == true)
  176. status = "1";
  177. else
  178. status = "2";
  179. con.Open();
  180. string sql = "Update Room set Number_Room ='" + numbertxt.Text + "', Telephone_Room = '" + telephonetxt.Text + "', Status_Room = '" + status.ToString() + "' where ID_Room = '" + idroomtxt.Text + "'";
  181. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  182. dataAdapter.SelectCommand.ExecuteNonQuery();
  183. con.Close();
  184. idroomtxt.Text = "";
  185. numbertxt.Text = "";
  186. telephonetxt.Text = "";
  187. yescheck.IsChecked = true;
  188. showgrid();
  189. MessageBox.Show("Комната изменена!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  190. }
  191. catch (Exception ex)
  192. {
  193. con.Close();
  194. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  195. }
  196. }
  197. }
  198. catch (Exception ex)
  199. {
  200. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  201. }
  202. }
  203. private void Delete_Click(object sender, RoutedEventArgs e)
  204. {
  205. if (idroomtxt.Text == "")
  206. {
  207. MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  208. }
  209. else
  210. {
  211. try
  212. {
  213. con.Open();
  214. string sql = "DELETE FROM Room WHERE ID_Room = '" + idroomtxt.Text + "'";
  215. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  216. dataAdapter.SelectCommand.ExecuteNonQuery();
  217. con.Close();
  218. idroomtxt.Text = "";
  219. numbertxt.Text = "";
  220. telephonetxt.Text = "";
  221. yescheck.IsChecked = true;
  222. showgrid();
  223. MessageBox.Show("Комната удалена!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
  224. }
  225. catch (Exception ex)
  226. {
  227. con.Close();
  228. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  229. }
  230. }
  231. }
  232. private void Grid_Loaded(object sender, RoutedEventArgs e)
  233. {
  234. showgrid();
  235. }
  236. void showgrid()
  237. {
  238. try
  239. {
  240. con.Open();
  241. string rke = "SELECT ID_Room, Number_Room, Telephone_Room, RoomStatus.Status From Room inner join RoomStatus on RoomStatus.ID_Status = Room.Status_Room";
  242. SqlDataAdapter dataAdapter = new SqlDataAdapter(rke, con);
  243. DataTable data = new DataTable("Room");
  244. dataAdapter.Fill(data);
  245. dataroom.ItemsSource = data.DefaultView;
  246. dataAdapter.Update(data);
  247. con.Close();
  248. dataroom.Columns[0].Header = "ID";
  249. dataroom.Columns[1].Header = "Номер комнты";
  250. dataroom.Columns[2].Header = "Телефон комнаты";
  251. dataroom.Columns[3].Header = "Статус комнаты";
  252. dataroom.Columns[0].Visibility = Visibility.Collapsed;
  253. }
  254. catch (Exception ex)
  255. {
  256. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  257. }
  258. }
  259. private void dataroom_SelectionChanged(object sender, SelectionChangedEventArgs e)
  260. {
  261. try
  262. {
  263. DataGrid gd = (DataGrid)sender;
  264. DataRowView rowView = gd.SelectedItem as DataRowView;
  265. if (rowView != null)
  266. {
  267. idroomtxt.Text = rowView["ID_Room"].ToString();
  268. numbertxt.Text = rowView["Number_Room"].ToString();
  269. telephonetxt.Text = rowView["Telephone_Room"].ToString();
  270. status = rowView["Status"].ToString();
  271. if (status == "Свободна")
  272. yescheck.IsChecked = true;
  273. else
  274. nocheck.IsChecked = true;
  275. }
  276. }
  277. catch (Exception ex)
  278. {
  279. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  280. }
  281. }
  282. private void Refresh_Click(object sender, RoutedEventArgs e)
  283. {
  284. showgrid();
  285. idroomtxt.Text = "";
  286. numbertxt.Text = "";
  287. telephonetxt.Text = "";
  288. yescheck.IsChecked = true;
  289. }
  290. }
  291. }