ClientRoom.xaml.cs 14 KB

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