Goal:
I am trying to get the hang of using MySQL connections within C# windows forms. These are such areas I am targeting:
- Loading dataGridView correctly
- Refresh dataGridView from an external form (See
public void RefreshGrid()
) - Update query for MySQL connection
I'd like to hear some reviews from you guys. Does the structure look good, can something be changed to, because of some irrelevant code?
This is what I have so far:
using System;
using System.Configuration;
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 MySql.Data.MySqlClient;
namespace Final_Version
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
//MySQL connection variable
private MySqlConnection connection;
//open connection to database
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
//When handling errors, you can your application's response based on the error number.
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator");
break;
default:
MessageBox.Show(ex.Message);
break;
}
return false;
}
}
//Close connection
private bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
//Adapter
private MySqlDataAdapter mySqlDataAdapter;
private void Form3_Load(object sender, EventArgs e)
{
// MySQL connection string
string connString = ConfigurationManager.ConnectionStrings["Final_Version.Properties.Settings.technicalConnectionString"].ConnectionString;
connection = new MySqlConnection(connString);
if (this.OpenConnection() == true)
{
//Logic
mySqlDataAdapter = new MySqlDataAdapter("select * from user", connection);
DataSet DS = new DataSet();
mySqlDataAdapter.Fill(DS);
dataGridView1.DataSource = DS.Tables[0];
//close connection
this.CloseConnection();
}
}
//Refresh Data Grid from external Forms
public void RefreshGrid()
{
// MySQL connection string
string connString = ConfigurationManager.ConnectionStrings["Final_Version.Properties.Settings.technicalConnectionString"].ConnectionString;
connection = new MySqlConnection(connString);
if (this.OpenConnection() == true)
{
//Logic
mySqlDataAdapter = new MySqlDataAdapter("select * from user", connection);
DataSet DS = new DataSet();
mySqlDataAdapter.Fill(DS);
dataGridView1.DataSource = DS.Tables[0];
//close connection
this.CloseConnection();
}
}
//Open new form *Form 4 - Add user*
private void button1_Click(object sender, EventArgs e)
{
var form4 = new Form4(this);
form4.Show();
}
//Remove user
private void button2_Click(object sender, EventArgs e)
{
var confirmResult = MessageBox.Show("Remove this user?",
"Confirm",
MessageBoxButtons.YesNo);
if (confirmResult == DialogResult.Yes)
{
if (this.dataGridView1.SelectedRows.Count > 0)
{
string user = dataGridView1.SelectedCells[0].Value.ToString();
string connString = ConfigurationManager.ConnectionStrings["Final_Version.Properties.Settings.technicalConnectionString"].ConnectionString;
MySqlConnection conn = new MySqlConnection(connString);
conn.Open();
MySqlCommand comm = conn.CreateCommand();
comm.CommandText = "delete from user where username = @user";
comm.Parameters.AddWithValue("@user", user);
comm.ExecuteNonQuery();
conn.Close();
MessageBox.Show("User (" + user + ") removed");
this.RefreshGrid();
}
else
{
MessageBox.Show("Select Row");
}
}
else
{
return;
}
}
}
}
EXPLAIN
? \$\endgroup\$