Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using MySql.Data.MySqlClient;
- namespace Cars
- {
- /// <summary>
- /// Логика взаимодействия для MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- string connStr = "server=localhost;user=root;database=carsdb;password=0000;";
- MySqlConnection conn = new MySqlConnection(connStr);
- conn.Open();
- string sql = "SELECT id, mark, model, price FROM cars";
- MySqlCommand command = new MySqlCommand(sql, conn);
- string last_id_db = "SELECT MAX(id) FROM cars";
- MySqlCommand commandLastId = new MySqlCommand(last_id_db, conn);
- string last_id = commandLastId.ExecuteScalar().ToString();
- MySqlDataReader reader = command.ExecuteReader();
- int[] array = new int[Convert.ToInt32(last_id)];
- int x = 0;
- while (reader.Read())
- {
- lstCars.Items.Add(reader[0].ToString() + " " + reader[1].ToString() + " " + reader[2].ToString() + " " + reader[3].ToString());
- array[x] = Convert.ToInt32(reader[0]);
- x++;
- }
- conn.Close();
- int x1 = x;
- x = 0;
- while (x < x1)
- {
- MessageBox.Show(Convert.ToString(array[x]));
- x++;
- }
- }
- private void cmdGetCar_Click(object sender, RoutedEventArgs e)
- {
- }
- private void cmdDeleteCar_Click(object sender, RoutedEventArgs e)
- {
- if (lstCars.SelectedIndex != -1)
- {
- int delete_Id = lstCars.SelectedIndex;
- string connStr = "server=localhost;user=root;database=carsdb;password=0000;";
- MySqlConnection conn = new MySqlConnection(connStr);
- conn.Open();
- string query = $"DELETE FROM cars WHERE id={delete_Id}";
- MySqlCommand MyCommand2 = new MySqlCommand(query, conn);
- MySqlDataReader MyReader2;
- MyReader2 = MyCommand2.ExecuteReader();
- MessageBox.Show("Data from Db Deleted");
- conn.Close();
- conn.Open();
- string sql = "SELECT id, mark, model, price FROM cars";
- MySqlCommand command = new MySqlCommand(sql, conn);
- MySqlDataReader reader = command.ExecuteReader();
- lstCars.Items.Clear();
- while (reader.Read())
- {
- lstCars.Items.Add(reader[0].ToString() + " " + reader[1].ToString() + " " + reader[2].ToString() + " " + reader[3].ToString());
- }
- conn.Close();
- }
- else
- MessageBox.Show("Select car please");
- }
- private void cmdAddCar_Click(object sender, RoutedEventArgs e)
- {
- CarCreate carCreate = new CarCreate();
- carCreate.Mark = Mark.Text;
- if (carCreate.Mark == "")
- {
- MessageBox.Show("Enter car mark please");
- }
- carCreate.Model = Model.Text;
- if (carCreate.Model == "")
- {
- MessageBox.Show("Enter car model please");
- }
- if (Price.Text != "")
- {
- carCreate.Price = Convert.ToInt32(Price.Text);
- }
- else MessageBox.Show("Enter car price please");
- if (carCreate.Mark != "" & carCreate.Model != "" & Convert.ToString(carCreate.Price) != "")
- {
- MessageBox.Show("Car added to base");
- string connStr = "server=localhost;user=root;database=carsdb;password=0000;";
- MySqlConnection conn = new MySqlConnection(connStr);
- conn.Open();
- string last_id_db = "SELECT MAX(id) FROM cars";
- MySqlCommand commandLastId = new MySqlCommand(last_id_db, conn);
- string last_id = commandLastId.ExecuteScalar().ToString();
- MessageBox.Show(last_id);
- int last_idN;
- last_idN = Convert.ToInt32(last_id);
- ++last_idN;
- string query = $"INSERT INTO cars (id, mark, model, price) VALUES ('{last_idN}','{carCreate.Mark}', '{carCreate.Model}', {carCreate.Price.ToString()})";
- MySqlCommand commandAdd = new MySqlCommand(query, conn);
- commandAdd.ExecuteNonQuery();
- string sql = "SELECT id, mark, model, price FROM cars";
- MySqlCommand command = new MySqlCommand(sql, conn);
- MySqlDataReader reader = command.ExecuteReader();
- lstCars.Items.Clear();
- while (reader.Read())
- {
- lstCars.Items.Add(reader[0].ToString() + " " + reader[1].ToString() + " " + reader[2].ToString() + " " + reader[3].ToString());
- }
- conn.Close();
- // lstCars.Items.Add(carCreate.Mark + " " + carCreate.Model + " " + carCreate.Price.ToString());
- }
- // MessageBox.Show(Convert.ToString(carCreate.Price));
- }
- private void cmdPreviousCar_Click(object sender, RoutedEventArgs e)
- {
- }
- private void cmdNextCar_Click(object sender, RoutedEventArgs e)
- {
- }
- private void Price_TextChanged(object sender, EventArgs e)
- {
- if (!double.TryParse(Price.Text, out double a))
- {
- Price.Text = "";
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement