Загрузка данных


using MySql.Data;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace magaz
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string table_name = "";
        void DataLoad1() {
            string connectionstr = "server=localhost;port=3306;database=magaz;user=root;password=root";
            string query = "SELECT id, name, second_name, email FROM pocupatel";
            DataTable table = new DataTable();
            using (MySqlConnection conn = new MySqlConnection(connectionstr)) { 
                conn.Open();
                MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn);
                adapter.Fill(table);
            }
            dataGridView1.DataSource = table;
            table_name = "pocupatel";
        }
        void DataLoad2()
        {
            string connectionstr = "server=localhost;port=3306;database=magaz;user=root;password=root";
            string query = "SELECT id, name, price, amount FROM product";
            DataTable table = new DataTable();
            using (MySqlConnection conn = new MySqlConnection(connectionstr))
            {
                conn.Open();
                MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn);
                adapter.Fill(table);
            }
            dataGridView1.DataSource = table;
            table_name = "product";
        }
        void DataLoad3()
        {
            string connectionstr = "server=localhost;port=3306;database=magaz;user=root;password=root";
            string query = "SELECT id, pokupatel_id, product_id, kol FROM zakaz";
            DataTable table = new DataTable();
            using (MySqlConnection conn = new MySqlConnection(connectionstr))
            {
                conn.Open();
                MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn);
                adapter.Fill(table);
            }
            dataGridView1.DataSource = table;
            table_name = "zakaz";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataLoad1();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataLoad2();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DataLoad3();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string connection_string = "server=localhost;port=3306;database=magaz;user=root;password=root";
            string query = "";
            if (table_name == "pocupatel")
            {
                query = $"INSERT INTO pocupatel(name, second_name, email) VALUES ('{textBox1.Text}','{textBox2.Text}','{textBox3.Text}')";
            }
            else if (table_name == "product") {
                query = $"INSERT INTO product(name, price, amount) VALUES ('{textBox1.Text}','{textBox2.Text}','{textBox3.Text}')";
            }
            else if (table_name == "zakaz")
            {
                query = $"INSERT INTO zakaz(pocupatel_id, product_id, kol) VALUES ('{textBox1.Text}','{textBox2.Text}','{textBox3.Text}')";
            }
            else
            {
                MessageBox.Show("Не выбрана таблица для добавления");
                return;
            }
            using (MySqlConnection connection = new MySqlConnection(connection_string)) {
                connection.Open();
                MySqlCommand cmd = new MySqlCommand(query, connection);
                cmd.ExecuteNonQuery();
            }
            if (table_name == "pocupatel")
            {
                DataLoad1();
            }
            else if (table_name == "product")
            {
                DataLoad2();
            }
            else if (table_name == "zakaz")
            {
                DataLoad3();
            }
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();

        }

        private void button6_Click(object sender, EventArgs e)
        {

        }

        private void button7_Click(object sender, EventArgs e)
        {

        }
    }
}