Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows.Forms;
- using MySql.Data.MySqlClient;
- namespace CRUDWindowsFormsApp
- {
- public partial class MainForm : Form
- {
- private string connectionString = "Server=myServerAddress;Database=myDatabase;User Id=myUsername;Password=myPassword;";
- public MainForm()
- {
- InitializeComponent();
- }
- private void MainForm_Load(object sender, EventArgs e)
- {
- LoadData();
- }
- private void LoadData()
- {
- using (MySqlConnection connection = new MySqlConnection(connectionString))
- {
- connection.Open();
- string selectQuery = "SELECT * FROM TableName";
- using (MySqlCommand cmd = new MySqlCommand(selectQuery, connection))
- {
- using (MySqlDataReader reader = cmd.ExecuteReader())
- {
- dataGridView.DataSource = null;
- dataGridView.Rows.Clear();
- while (reader.Read())
- {
- dataGridView.Rows.Add(reader["ID"], reader["Name"], reader["Age"]);
- }
- }
- }
- }
- }
- private void btnCreate_Click(object sender, EventArgs e)
- {
- string name = txtName.Text;
- int age;
- if (int.TryParse(txtAge.Text, out age))
- {
- using (MySqlConnection connection = new MySqlConnection(connectionString))
- {
- connection.Open();
- string insertQuery = "INSERT INTO TableName (Name, Age) VALUES (@Name, @Age)";
- using (MySqlCommand cmd = new MySqlCommand(insertQuery, connection))
- {
- cmd.Parameters.AddWithValue("@Name", name);
- cmd.Parameters.AddWithValue("@Age", age);
- cmd.ExecuteNonQuery();
- MessageBox.Show("Record created successfully!");
- LoadData();
- }
- }
- }
- else
- {
- MessageBox.Show("Please enter a valid age.");
- }
- }
- private void btnUpdate_Click(object sender, EventArgs e)
- {
- int id;
- if (int.TryParse(txtID.Text, out id))
- {
- string name = txtName.Text;
- int age;
- if (int.TryParse(txtAge.Text, out age))
- {
- using (MySqlConnection connection = new MySqlConnection(connectionString))
- {
- connection.Open();
- string updateQuery = "UPDATE TableName SET Name = @Name, Age = @Age WHERE ID = @ID";
- using (MySqlCommand cmd = new MySqlCommand(updateQuery, connection))
- {
- cmd.Parameters.AddWithValue("@Name", name);
- cmd.Parameters.AddWithValue("@Age", age);
- cmd.Parameters.AddWithValue("@ID", id);
- cmd.ExecuteNonQuery();
- MessageBox.Show("Record updated successfully!");
- LoadData();
- }
- }
- }
- else
- {
- MessageBox.Show("Please enter a valid age.");
- }
- }
- else
- {
- MessageBox.Show("Please enter a valid ID.");
- }
- }
- private void btnDelete_Click(object sender, EventArgs e)
- {
- int id;
- if (int.TryParse(txtID.Text, out id))
- {
- using (MySqlConnection connection = new MySqlConnection(connectionString))
- {
- connection.Open();
- string deleteQuery = "DELETE FROM TableName WHERE ID = @ID";
- using (MySqlCommand cmd = new MySqlCommand(deleteQuery, connection))
- {
- cmd.Parameters.AddWithValue("@ID", id);
- cmd.ExecuteNonQuery();
- MessageBox.Show("Record deleted successfully!");
- LoadData();
- }
- }
- }
- else
- {
- MessageBox.Show("Please enter a valid ID.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement