Advertisement
Guest User

Untitled

a guest
Feb 26th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  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.Navigation;
  14. using System.Windows.Shapes;
  15. using MySql.Data.MySqlClient;
  16.  
  17. namespace Cars
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для MainWindow.xaml
  21. /// </summary>
  22. public partial class MainWindow : Window
  23. {
  24. public MainWindow()
  25. {
  26. InitializeComponent();
  27. string connStr = "server=localhost;user=root;database=carsdb;password=0000;";
  28. MySqlConnection conn = new MySqlConnection(connStr);
  29. conn.Open();
  30.  
  31. string sql = "SELECT id, mark, model, price FROM cars";
  32. MySqlCommand command = new MySqlCommand(sql, conn);
  33.  
  34. string last_id_db = "SELECT MAX(id) FROM cars";
  35. MySqlCommand commandLastId = new MySqlCommand(last_id_db, conn);
  36. string last_id = commandLastId.ExecuteScalar().ToString();
  37.  
  38. MySqlDataReader reader = command.ExecuteReader();
  39. int[] array = new int[Convert.ToInt32(last_id)];
  40. int x = 0;
  41. while (reader.Read())
  42. {
  43. lstCars.Items.Add(reader[0].ToString() + " " + reader[1].ToString() + " " + reader[2].ToString() + " " + reader[3].ToString());
  44. array[x] = Convert.ToInt32(reader[0]);
  45. x++;
  46. }
  47.  
  48. conn.Close();
  49.  
  50. int x1 = x;
  51. x = 0;
  52. while (x < x1)
  53. {
  54. MessageBox.Show(Convert.ToString(array[x]));
  55. x++;
  56.  
  57. }
  58.  
  59. }
  60.  
  61.  
  62. private void cmdGetCar_Click(object sender, RoutedEventArgs e)
  63. {
  64.  
  65. }
  66.  
  67. private void cmdDeleteCar_Click(object sender, RoutedEventArgs e)
  68. {
  69.  
  70. if (lstCars.SelectedIndex != -1)
  71. {
  72. int delete_Id = lstCars.SelectedIndex;
  73.  
  74. string connStr = "server=localhost;user=root;database=carsdb;password=0000;";
  75. MySqlConnection conn = new MySqlConnection(connStr);
  76. conn.Open();
  77.  
  78. string query = $"DELETE FROM cars WHERE id={delete_Id}";
  79. MySqlCommand MyCommand2 = new MySqlCommand(query, conn);
  80. MySqlDataReader MyReader2;
  81.  
  82. MyReader2 = MyCommand2.ExecuteReader();
  83. MessageBox.Show("Data from Db Deleted");
  84. conn.Close();
  85. conn.Open();
  86. string sql = "SELECT id, mark, model, price FROM cars";
  87. MySqlCommand command = new MySqlCommand(sql, conn);
  88. MySqlDataReader reader = command.ExecuteReader();
  89.  
  90. lstCars.Items.Clear();
  91. while (reader.Read())
  92. {
  93. lstCars.Items.Add(reader[0].ToString() + " " + reader[1].ToString() + " " + reader[2].ToString() + " " + reader[3].ToString());
  94. }
  95.  
  96. conn.Close();
  97. }
  98. else
  99. MessageBox.Show("Select car please");
  100.  
  101. }
  102.  
  103. private void cmdAddCar_Click(object sender, RoutedEventArgs e)
  104. {
  105. CarCreate carCreate = new CarCreate();
  106. carCreate.Mark = Mark.Text;
  107. if (carCreate.Mark == "")
  108. {
  109. MessageBox.Show("Enter car mark please");
  110. }
  111. carCreate.Model = Model.Text;
  112. if (carCreate.Model == "")
  113. {
  114. MessageBox.Show("Enter car model please");
  115. }
  116. if (Price.Text != "")
  117. {
  118. carCreate.Price = Convert.ToInt32(Price.Text);
  119. }
  120. else MessageBox.Show("Enter car price please");
  121.  
  122.  
  123. if (carCreate.Mark != "" & carCreate.Model != "" & Convert.ToString(carCreate.Price) != "")
  124. {
  125.  
  126. MessageBox.Show("Car added to base");
  127.  
  128. string connStr = "server=localhost;user=root;database=carsdb;password=0000;";
  129. MySqlConnection conn = new MySqlConnection(connStr);
  130. conn.Open();
  131.  
  132. string last_id_db = "SELECT MAX(id) FROM cars";
  133. MySqlCommand commandLastId = new MySqlCommand(last_id_db, conn);
  134. string last_id = commandLastId.ExecuteScalar().ToString();
  135. MessageBox.Show(last_id);
  136. int last_idN;
  137. last_idN = Convert.ToInt32(last_id);
  138. ++last_idN;
  139.  
  140. string query = $"INSERT INTO cars (id, mark, model, price) VALUES ('{last_idN}','{carCreate.Mark}', '{carCreate.Model}', {carCreate.Price.ToString()})";
  141. MySqlCommand commandAdd = new MySqlCommand(query, conn);
  142.  
  143. commandAdd.ExecuteNonQuery();
  144.  
  145.  
  146. string sql = "SELECT id, mark, model, price FROM cars";
  147. MySqlCommand command = new MySqlCommand(sql, conn);
  148.  
  149. MySqlDataReader reader = command.ExecuteReader();
  150. lstCars.Items.Clear();
  151. while (reader.Read())
  152. {
  153.  
  154. lstCars.Items.Add(reader[0].ToString() + " " + reader[1].ToString() + " " + reader[2].ToString() + " " + reader[3].ToString());
  155. }
  156.  
  157.  
  158.  
  159.  
  160.  
  161. conn.Close();
  162.  
  163.  
  164. // lstCars.Items.Add(carCreate.Mark + " " + carCreate.Model + " " + carCreate.Price.ToString());
  165.  
  166. }
  167.  
  168. // MessageBox.Show(Convert.ToString(carCreate.Price));
  169.  
  170.  
  171. }
  172.  
  173. private void cmdPreviousCar_Click(object sender, RoutedEventArgs e)
  174. {
  175.  
  176. }
  177.  
  178. private void cmdNextCar_Click(object sender, RoutedEventArgs e)
  179. {
  180.  
  181. }
  182.  
  183. private void Price_TextChanged(object sender, EventArgs e)
  184. {
  185. if (!double.TryParse(Price.Text, out double a))
  186. {
  187. Price.Text = "";
  188. }
  189. }
  190.  
  191.  
  192. }
  193.  
  194.  
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement