Database-Connection
Database-Connection
using MySql.Data.MySqlClient;
using System.Data;
namespace Student_Information
{
public partial class Form1 : Form
{
DatabaseConnection dbConn = new DatabaseConnection();
MySqlConnection conn;
MySqlCommand cmd;
MySqlDataReader reader;
MySqlDataAdapter da;
DataTable dt;
public Form1()
{
InitializeComponent();
}
conn.Close();
}
refreshData();
}
refreshData();
clear();
conn.Close();
}
}
refreshData();
clear();
conn.Close();
}
}
}
else
{
textBox1.Text = dataGridView1.CurrentRow.Cells["student_id"].Value.ToString();
textBox2.Text = dataGridView1.CurrentRow.Cells["last_name"].Value.ToString();
textBox3.Text = dataGridView1.CurrentRow.Cells["first_name"].Value.ToString();
textBox4.Text = dataGridView1.CurrentRow.Cells["middle_name"].Value.ToString();
numericUpDown1.Value = int.Parse(dataGridView1.CurrentRow.Cells["age"].Value.ToString());
button1.Enabled = false;
button2.Enabled = true;
textBox1.ReadOnly = true;
}
}
}
else
{
conn = dbConn.connect();
string sql = "DELETE FROM tbl_studentinfo WHERE student_id='" +
dataGridView1.CurrentRow.Cells["student_id"].Value.ToString() + "'";
cmd = new MySqlCommand(sql, conn);
reader = cmd.ExecuteReader();
refreshData();
clear();
conn.Close();
}
}
private void button5_Click(object sender, EventArgs e)
{
clear();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using MySql.Data.MySqlClient;
namespace Student_Information
{
internal class DatabaseConnection
{
public MySqlConnection connect()
{
string host = "localhost";
string username = "root";
string password = "";
string database = "crud_db";
string connectionString =
$"Server={host};Database={database};User={username};Password=\"{password}\";";
try
{
MySqlConnection conn = new MySqlConnection(connectionString);
conn.Open();
Console.WriteLine("Connection successful.");
return conn;
}
catch (MySqlException ex)
{
Console.WriteLine($"Connection error: {ex.Message}");
return null;
}
}
}
}