using System;
using System.Data;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Test
{
public partial class Form1 : Form
{
string cs = "Server=127.0.0.1;Port=3306;Database=TESTT;Uid=root;Pwd=sokolovs;Charset=utf8;";
public Form1() { InitializeComponent(); }
void UpdateGrid()
{
try
{
MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM Kino", cs);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (Exception ex) { MessageBox.Show("Ошибка: " + ex.Message); }
}
private void Form1_Load(object sender, EventArgs e) { UpdateGrid(); }
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { }
private void label1_Click(object sender, EventArgs e) { }
private void label2_Click(object sender, EventArgs e) { }
private void textBox1_TextChanged(object sender, EventArgs e) { }
private void textBox2_TextChanged(object sender, EventArgs e) { }
private void button1_Click(object sender, EventArgs e)
{
try
{
using (MySqlConnection con = new MySqlConnection(cs))
{
MySqlCommand cmd = new MySqlCommand("INSERT INTO Kino (Name, Info) VALUES (@n, @i)", con);
cmd.Parameters.AddWithValue("@n", textBox1.Text);
cmd.Parameters.AddWithValue("@i", textBox2.Text);
con.Open(); cmd.ExecuteNonQuery();
}
textBox1.Clear(); textBox2.Clear(); UpdateGrid();
}
catch (Exception ex) { MessageBox.Show("Ошибка: " + ex.Message); }
}
private void button2_Click(object sender, EventArgs e)
{
try
{
int id = Convert.ToInt32(dataGridView1.CurrentRow.Cells["ID"].Value);
using (MySqlConnection con = new MySqlConnection(cs))
{
MySqlCommand cmd = new MySqlCommand("DELETE FROM Kino WHERE ID = @id", con);
cmd.Parameters.AddWithValue("@id", id);
con.Open(); cmd.ExecuteNonQuery();
}
UpdateGrid();
}
catch (Exception ex) { MessageBox.Show("Ошибка: " + ex.Message); }
}
}
}