-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathc#-mysql-crud.cs
150 lines (140 loc) · 5.31 KB
/
c#-mysql-crud.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Data;
using MySql.Data.MySqlClient;
namespace YourNamespace
{
/// <summary>
/// Represents a Database connection and provides CRUD operations.
///
/// Author: Mokter Hossain
/// Email: [email protected]
/// Website: www.gglink.uk
/// Github: https://github.com/moktermd08
/// Linkedin: https://www.linkedin.com/in/mr-mokter/
/// Twitter: https://twitter.com/moGGLink
/// </summary>
public class Database : IDisposable
{
private readonly string _connectionString;
private MySqlConnection _connection;
/// <summary>
/// Initializes a new instance of the Database class with connection parameters.
/// </summary>
/// <param name="host">Database host</param>
/// <param name="user">Database user</param>
/// <param name="password">Database password</param>
/// <param name="database">Database name</param>
public Database(string host = "localhost", string user = "user", string password = "password", string database = "database")
{
_connectionString = $"Server={host};Database={database};User ID={user};Password={password};";
}
/// <summary>
/// Opens a connection to the database.
/// </summary>
/// <returns>True if the connection is successful; otherwise, False.</returns>
public bool Connect()
{
_connection = new MySqlConnection(_connectionString);
try
{
_connection.Open();
return true;
}
catch (MySqlException ex)
{
Console.WriteLine("MySQL Connection Error: " + ex.Message);
return false;
}
}
/// <summary>
/// Closes the connection to the database.
/// </summary>
public void Disconnect()
{
if (_connection != null && _connection.State == ConnectionState.Open)
{
_connection.Close();
}
}
/// <summary>
/// Executes a SELECT query and returns the result as a DataTable.
/// </summary>
/// <param name="query">The SQL query string</param>
/// <returns>A DataTable containing the result set</returns>
public DataTable Select(string query)
{
using (var command = new MySqlCommand(query, _connection))
{
using (var adapter = new MySqlDataAdapter(command))
{
var result = new DataTable();
adapter.Fill(result);
return result;
}
}
}
/// <summary>
/// Inserts a new record into a table.
/// </summary>
/// <param name="table">The name of the table</param>
/// <param name="columns">The columns for the insert</param>
/// <param name="values">The values for the insert</param>
/// <returns>True if the insert was successful; otherwise, False.</returns>
public bool Insert(string table, string columns, string values)
{
var query = $"INSERT INTO {table} ({columns}) VALUES ({values})";
return ExecuteNonQuery(query);
}
/// <summary>
/// Updates records in a table.
/// </summary>
/// <param name="table">The name of the table</param>
/// <param name="setClause">The SET clause for the update</param>
/// <param name="whereClause">The WHERE clause for the update</param>
/// <returns>True if the update was successful; otherwise, False.</returns>
public bool Update(string table, string setClause, string whereClause)
{
var query = $"UPDATE {table} SET {setClause} WHERE {whereClause}";
return ExecuteNonQuery(query);
}
/// <summary>
/// Deletes records from a table.
/// </summary>
/// <param name="table">The name of the table</param>
/// <param name="whereClause">The WHERE clause for the delete</param>
/// <returns>True if the delete was successful; otherwise, False.</returns>
public bool Delete(string table, string whereClause)
{
var query = $"DELETE FROM {table} WHERE {whereClause}";
return ExecuteNonQuery(query);
}
/// <summary>
/// Executes an SQL query that does not return a result set.
/// </summary>
/// <param name="query">The SQL query string</param>
/// <returns>True if the query was executed successfully; otherwise, False.</returns>
private bool ExecuteNonQuery(string query)
{
using (var command = new MySqlCommand(query, _connection))
{
try
{
command.ExecuteNonQuery();
return true;
}
catch (MySqlException ex)
{
Console.WriteLine("MySQL Query Error: " + ex.Message);
return false;
}
}
}
/// <summary>
/// Implements the IDisposable interface to allow for using statement support and manual resource management.
/// </summary>
public void Dispose()
{
Disconnect();
}
}
}