top of page

Используя  готовый проект по работе с базой данных MySQL подготовить свою базу и таблицу для работы с данными

Так выглядит форма для работы с таблицей

Лабораторная работа №10.  Работы с базами данных

Исходный текст программы Form1.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using MySql.Data.MySqlClient;

 

namespace WindowsFormsApplication16

{

   

public partial class Form1 : Form

       

    {

    private BindingSource bindingSorce = new BindingSource();

 

        public Form1()

           

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

          

            string connStr = "server=127.0.0.1;user=root;password=123456;database=mctestdb";

           

            MySqlConnection coon = new MySqlConnection(connStr);

            MySqlCommand cmd = new MySqlCommand();

            cmd.Connection = coon;

            cmd.CommandText = "SELECT * FROM dm_autos";

            MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);

            DataSet dataset = new DataSet();

            adapter.Fill(dataset);

            bindingSorce.DataSource = dataset.Tables[0];

            dataGridView1.DataSource = bindingSorce;

            coon.Close();

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            string connStr = "server=127.0.0.1;user=root;password=123456;database=mctestdb";

            MySqlConnection coon = new MySqlConnection(connStr);

            MySqlCommand cmd = new MySqlCommand();

            cmd.Connection = coon;

            cmd.CommandText = "INSERT INTO dm_autos (model,drive,rudder,gearbox) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";

            MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);

            DataSet dataset = new DataSet();

            adapter.Fill(dataset);

            coon.Close();

           

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            string select = dataGridView1[dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex].Value.ToString();

            string connStr = "server=127.0.0.1;user=root;password=123456;database=mctestdb";

            MySqlConnection coon = new MySqlConnection(connStr);

            MySqlCommand cmd = new MySqlCommand();

            cmd.Connection = coon;

            cmd.CommandText = "DELETE from dm_autos WHERE id=" + select + "";

            MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);

            DataSet dataset = new DataSet();

            adapter.Fill(dataset);

            coon.Close();

        }

bottom of page