ClientRoom.xaml.cs 13 KB

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