ClientRoom.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. using System.Windows.Threading;
  15. using System.Data;
  16. using System.Data.SqlClient;
  17. namespace HotelCalifornia
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для ClientRoom.xaml
  21. /// </summary>
  22. public partial class ClientRoom : Window
  23. {
  24. public ClientRoom()
  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 Close(object sender, RoutedEventArgs e)
  53. {
  54. Application.Current.Shutdown();
  55. }
  56. //Свернуть окно
  57. private void WindMin_Click(object sender, RoutedEventArgs e)
  58. {
  59. this.WindowState = WindowState.Minimized;
  60. }
  61. //Возврат к окну выбора функции
  62. private void Back(object sender, RoutedEventArgs e)
  63. {
  64. Variant variant = new Variant();
  65. variant.idadmintxt.Text = idadmintxt.Text;
  66. this.Close();
  67. variant.Show();
  68. }
  69. //Выбор строки из БД
  70. private void dataClientRoom_SelectionChanged(object sender, SelectionChangedEventArgs e)
  71. {
  72. try
  73. {
  74. DataGrid gd = (DataGrid)sender;
  75. DataRowView rowView = gd.SelectedItem as DataRowView;
  76. if (rowView != null)
  77. {
  78. roomcombo.Text = rowView["Number_Room"].ToString();
  79. clientcombo.Text = rowView["LastName_Client"].ToString();
  80. }
  81. }
  82. catch (Exception ex)
  83. {
  84. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  85. }
  86. }
  87. //Добавление заселения
  88. private void Add_Click(object sender, RoutedEventArgs e)
  89. {
  90. try
  91. {
  92. if (clientcombo.Text == "" || roomcombo.Text == "")
  93. {
  94. MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  95. }
  96. else
  97. {
  98. con.Open();
  99. SqlCommand cmd = new SqlCommand("Select * from Room where Number_Room ='" + roomcombo.Text + "'", con);
  100. cmd.CommandType = CommandType.Text;
  101. SqlDataAdapter adapter = new SqlDataAdapter();
  102. adapter.SelectCommand = cmd;
  103. DataSet dataSet = new DataSet();
  104. adapter.Fill(dataSet);
  105. if (dataSet.Tables[0].Rows.Count > 0)
  106. {
  107. string idroom = dataSet.Tables[0].Rows[0]["ID_Room"].ToString();
  108. SqlCommand cmd1 = new SqlCommand("Select * from Client where LastName_Client ='" + clientcombo.Text + "'", con);
  109. cmd1.CommandType = CommandType.Text;
  110. SqlDataAdapter adapter1 = new SqlDataAdapter();
  111. adapter1.SelectCommand = cmd1;
  112. DataSet dataSet1 = new DataSet();
  113. adapter1.Fill(dataSet1);
  114. if (dataSet1.Tables[0].Rows.Count > 0)
  115. {
  116. string idclient = dataSet1.Tables[0].Rows[0]["ID_Client"].ToString();
  117. SqlCommand cmd2 = new SqlCommand("Select * from RoomClient where ID_Client = '"+ idclient.ToString() +"'", con);
  118. cmd2.CommandType = CommandType.Text;
  119. SqlDataAdapter adapter2 = new SqlDataAdapter();
  120. adapter2.SelectCommand = cmd2;
  121. DataSet dataSet2 = new DataSet();
  122. adapter2.Fill(dataSet2);
  123. if (dataSet2.Tables[0].Rows.Count > 0)
  124. {
  125. con.Close();
  126. MessageBox.Show("У клиента уже есть комната!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  127. }
  128. else
  129. {
  130. string sql = "INSERT INTO RoomClient (ID_Room,ID_Client) VALUES('" + idroom.ToString() + "','" + idclient.ToString() + "')";
  131. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  132. dataAdapter.SelectCommand.ExecuteNonQuery();
  133. con.Close();
  134. showgrid();
  135. roomcombo.Text = "";
  136. clientcombo.Text = "";
  137. MessageBox.Show("Клиент заселен в комнату!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  138. }
  139. }
  140. }
  141. }
  142. }
  143. catch (Exception ex)
  144. {
  145. con.Close();
  146. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  147. }
  148. }
  149. //Удаление заселения
  150. private void Delete_Click(object sender, RoutedEventArgs e)
  151. {
  152. if (roomcombo.Text == "" || clientcombo.Text == "")
  153. {
  154. MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  155. }
  156. else
  157. {
  158. try
  159. {
  160. con.Open();
  161. SqlCommand cmd = new SqlCommand("Select * from Room where Number_Room ='" + roomcombo.Text + "'", con);
  162. cmd.CommandType = CommandType.Text;
  163. SqlDataAdapter adapter = new SqlDataAdapter();
  164. adapter.SelectCommand = cmd;
  165. DataSet dataSet = new DataSet();
  166. adapter.Fill(dataSet);
  167. if (dataSet.Tables[0].Rows.Count > 0)
  168. {
  169. string idroom = dataSet.Tables[0].Rows[0]["ID_Room"].ToString();
  170. SqlCommand cmd1 = new SqlCommand("Select * from Client where LastName_Client ='" + clientcombo.Text + "'", con);
  171. cmd1.CommandType = CommandType.Text;
  172. SqlDataAdapter adapter1 = new SqlDataAdapter();
  173. adapter1.SelectCommand = cmd1;
  174. DataSet dataSet1 = new DataSet();
  175. adapter1.Fill(dataSet1);
  176. if (dataSet1.Tables[0].Rows.Count > 0)
  177. {
  178. string idclient = dataSet1.Tables[0].Rows[0]["ID_Client"].ToString();
  179. SqlCommand cmd2 = new SqlCommand("Select * from RoomClient where ID_Client = '" + idclient.ToString() + "' and ID_Room = '" + idroom.ToString() + "'", con);
  180. cmd2.CommandType = CommandType.Text;
  181. SqlDataAdapter adapter2 = new SqlDataAdapter();
  182. adapter2.SelectCommand = cmd2;
  183. DataSet dataSet2 = new DataSet();
  184. adapter2.Fill(dataSet2);
  185. if (dataSet2.Tables[0].Rows.Count > 0)
  186. {
  187. string sql = "DELETE FROM RoomClient WHERE ID_Room = '" + idroom.ToString() + "' and ID_Client = '"+idclient.ToString()+"'; Update Rezervirovanie set ID_Status = '2' where ID_Client = '"+idclient.ToString()+"'; Update Room set Status_Room = '1' Where ID_Room = '"+idroom.ToString()+"'";
  188. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  189. dataAdapter.SelectCommand.ExecuteNonQuery();
  190. con.Close();
  191. showgrid();
  192. roomcombo.Text = "";
  193. clientcombo.Text = "";
  194. MessageBox.Show("Запись удалена!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  195. }
  196. else
  197. {
  198. con.Close();
  199. roomcombo.Text = "";
  200. clientcombo.Text = "";
  201. MessageBox.Show("Такой записи нет!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  202. }
  203. }
  204. }
  205. }
  206. catch (Exception ex)
  207. {
  208. con.Close();
  209. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  210. }
  211. }
  212. }
  213. //Запуск в самом начале
  214. private void Window_Loaded(object sender, RoutedEventArgs e)
  215. {
  216. fillroomcombo();
  217. fillclientcombo();
  218. showgrid();
  219. }
  220. //Фомировка данных из БД
  221. void showgrid()
  222. {
  223. try
  224. {
  225. con.Open();
  226. string sql = "SELECT Number_Room, Client.LastName_Client From Room inner join RoomClient on Room.ID_Room = RoomClient.ID_Room inner join Client on RoomClient.ID_Client = Client.ID_Client";
  227. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  228. DataTable data = new DataTable("RoomClient");
  229. dataAdapter.Fill(data);
  230. dataclientroom.ItemsSource = data.DefaultView;
  231. dataAdapter.Update(data);
  232. con.Close();
  233. dataclientroom.Columns[0].Header = "Номер комнты";
  234. dataclientroom.Columns[1].Header = "Фамилия клиента";
  235. }
  236. catch (Exception ex)
  237. {
  238. con.Close();
  239. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  240. }
  241. }
  242. //Заполнение combo
  243. #region Combo
  244. //Combo комнаты
  245. void fillroomcombo()
  246. {
  247. try
  248. {
  249. roomcombo.Items.Clear();
  250. con.Open();
  251. SqlCommand sql = con.CreateCommand();
  252. sql.CommandType = CommandType.Text;
  253. sql.CommandText = "Select Number_Room from Room WHERE Status_Room = 2";
  254. sql.ExecuteNonQuery();
  255. DataTable dt = new DataTable();
  256. SqlDataAdapter da = new SqlDataAdapter(sql);
  257. da.Fill(dt);
  258. foreach (DataRow dr in dt.Rows)
  259. {
  260. roomcombo.Items.Add(dr["Number_Room"].ToString());
  261. }
  262. con.Close();
  263. }
  264. catch (Exception ex)
  265. {
  266. con.Close();
  267. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  268. }
  269. }
  270. //Combo клиента
  271. void fillclientcombo()
  272. {
  273. try
  274. {
  275. clientcombo.Items.Clear();
  276. con.Open();
  277. SqlCommand sql = con.CreateCommand();
  278. sql.CommandType = CommandType.Text;
  279. sql.CommandText = "Select LastName_Client from Client";
  280. sql.ExecuteNonQuery();
  281. DataTable dt = new DataTable();
  282. SqlDataAdapter da = new SqlDataAdapter(sql);
  283. da.Fill(dt);
  284. foreach (DataRow dr in dt.Rows)
  285. {
  286. clientcombo.Items.Add(dr["LastName_Client"].ToString());
  287. }
  288. con.Close();
  289. }
  290. catch (Exception ex)
  291. {
  292. con.Close();
  293. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  294. }
  295. }
  296. #endregion
  297. //Обновление
  298. private void Refresh_Click(object sender, RoutedEventArgs e)
  299. {
  300. showgrid();
  301. roomcombo.Text = "";
  302. clientcombo.Text = "";
  303. }
  304. }
  305. }