-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseHelper.cs
85 lines (77 loc) · 3.2 KB
/
DatabaseHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Auth: John O'Neal
// Date: 04/07/2024
// Desc: Helper class for interacting with the database. This class contains methods for executing select and non-select queries, binding data to a DataGridView, and displaying error messages to the user.
//using MySql.Data.MySqlClient;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
namespace TaxApp_v2
{
public class DatabaseHelper
{
private static string connectionString = "Data Source=localhost;Initial Catalog=tax_app_db;uId=root;password=IowaLakesCIS-332";
// Generic method to bind any table to a DataGridView
/* public static void BindDatabaseToDataGridView(string tableName, DataGridView dataGridView)
{
string query = $"SELECT * FROM {tableName}";
DataTable table = ExecuteSelectQuery(query);
dataGridView.DataSource = table;
}*/
// Generic method to set the column headers of a DataGridView
public static void SetColumnHeaders(DataGridView dataGridView, List<string> headers)
{
for (int i = 0; i < headers.Count; i++)
{
dataGridView.Columns[i].HeaderText = headers[i];
}
}
// Method to execute a non-select query (INSERT, UPDATE, DELETE)
/*public static void ExecuteNonSelectQuery(string query, Dictionary<string, object> parameters = null)
{
using (MySqlConnection con = new MySqlConnection(connectionString))
{
con.Open();
using (MySqlCommand cmd = new MySqlCommand(query, con))
{
if (parameters != null)
{
foreach (var param in parameters)
{
cmd.Parameters.AddWithValue(param.Key, param.Value);
}
}
cmd.ExecuteNonQuery();
}
}
}
// Method to execute a select query and return the result as a DataTable
public static DataTable ExecuteSelectQuery(string query, Dictionary<string, object> parameters = null)
{
using (MySqlConnection con = new MySqlConnection(connectionString))
{
con.Open();
using (MySqlCommand cmd = new MySqlCommand(query, con))
{
if (parameters != null)
{
foreach (var param in parameters)
{
cmd.Parameters.AddWithValue(param.Key, param.Value);
}
}
using (MySqlDataReader dr = cmd.ExecuteReader())
{
DataTable dt = new DataTable();
dt.Load(dr);
return dt;
}
}
}
}*/
// Show a generic database error message to the user.
public static void ShowDatabaseErrorMessage()
{
MessageBox.Show("An error occurred with the database while processing your request.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}