Learning/Mysql-example/Database/DBConnectGeneral.cs
2023-05-05 20:58:11 +02:00

241 lines
8.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Google.Protobuf.WellKnownTypes;
using MySql.Data.MySqlClient;
using Mysql_example.Database.Schema;
using MySqlX.XDevAPI.Common;
using MySqlX.XDevAPI.Relational;
using static System.ComponentModel.Design.ObjectSelectorEditor;
using static System.Runtime.InteropServices.JavaScript.JSType;
using static Mysql_example.Database.DBConnectGeneral;
namespace Mysql_example.Database
{
class DBConnectGeneral
{
public MySqlConnection connection;
private string server;
private string database;
private string uid;
private string password;
public DBConnectGeneral()
{
this.server = Config.DBHost;
this.database= Config.DBName;
this.uid= Config.DBUser;
this.password = Config.DBPass;
Initialize();
}
//Connection Handeling
private void Initialize()
{
string connectionPayload = $"SERVER={server};DATABASE={database};UID={uid};PASSWORD={password};";
connection = new MySqlConnection(connectionPayload);
}
public bool OpenConnection()
{
try
{
connection.Open();
return true;
} catch (MySqlException exception)
{
switch (exception.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator");
break;
case 1045:
MessageBox.Show("Invalid username/password, please try again");
break;
default:
MessageBox.Show($"Error: {exception.Message}");
break;
}
return false;
}
}
public bool CloseConnection()
{
try
{
connection.Close();
return true;
} catch (MySqlException exception)
{
MessageBox.Show(exception.Message);
return false;
}
}
// Getting Information
public List<string> [] Select(string table, List<string> fields)
{
string querry = "SELECT ";
foreach (string field in fields)
{
querry += $"`{field}`, ";
}
querry = querry.Substring(0, querry.Length - 2);
querry += $" FROM `{table}`";
List<string>[] response = new List<string>[fields.Count];
for (int i = 0; i < fields.Count; i++)
{
response[i] = new List<string>();
}
if(this.OpenConnection())
{
MySqlCommand selectCmd = new MySqlCommand(querry, connection);
MySqlDataReader dataReader= selectCmd.ExecuteReader();
while (dataReader.Read())
{
for (int i = 0; i < fields.Count; i++)
{
response[i].Add(dataReader[fields[i]] + "");
}
}
dataReader.Close();
CloseConnection();
}
return response;
}
public List<string>[] Select(string table, List<string> fields, string selector)
{
string querry = "SELECT ";
foreach (string field in fields)
{
querry += $"`{field}`, ";
}
querry = querry.Substring(0, querry.Length - 2);
querry += $" FROM `{table}` WHERE {selector}";
List<string>[] response = new List<string>[fields.Count];
for (int i = 0; i < fields.Count; i++)
{
response[i] = new List<string>();
}
if (this.OpenConnection())
{
MySqlCommand selectCmd = new MySqlCommand(querry, connection);
MySqlDataReader dataReader = selectCmd.ExecuteReader();
while (dataReader.Read())
{
for (int i = 0; i < fields.Count; i++)
{
response[i].Add(dataReader[fields[i]] + "");
}
}
dataReader.Close();
CloseConnection();
}
return response;
}
// Modifying Information
public int Update(string table, List<string> fields, List<object> values, string selector)
{
string querry = $"UPDATE `{table}` SET ";
if (fields.Count != values.Count) throw new Exception("Count of Fields and Values to Update doesnt match");
for(int i = 0; i < fields.Count;i++)
{
string value = values[i] != null ? "'" + values[i].ToString() + "'" : "NULL";
querry += $"`{fields[i]}`= {value},";
}
querry = querry.Remove(querry.Length -1);
querry += $" WHERE {selector};";
if (this.OpenConnection())
{
MySqlCommand updateCommand = new MySqlCommand();
updateCommand.CommandText = querry;
updateCommand.Connection = connection;
try
{
int affected = updateCommand.ExecuteNonQuery();
return affected;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "There Was an Error Trying to Update.");
} finally
{
CloseConnection();
}
}
return 0;
}
public UInt64 Insert(string table, List<string> fields, List<object> values)
{
if (fields.Count != values.Count) throw new Exception("Count of Fields and Values to Update doesnt match");
string querry = $"INSERT INTO `{table}` (";
for (int i = 0; i < fields.Count; i++)
{
querry += $"`{fields[i]}`,";
}
querry = querry.Remove(querry.Length - 1);
querry += $") VALUES (";
for (int i = 0; i < values.Count; i++)
{
string value = values[i] != null ? "'" + values[i].ToString() + "'" : "NULL";
querry += $"{value},";
}
querry = querry.Remove(querry.Length - 1);
querry += ");";
if (this.OpenConnection())
{
MySqlCommand insertCommand = new MySqlCommand();
insertCommand.CommandText = querry;
insertCommand.Connection = connection;
try
{
insertCommand.ExecuteNonQuery();
return insertCommand.LastInsertedId < 0 ? 0 : (UInt64)insertCommand.LastInsertedId;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "There Was an Error Trying to Insert.");
}
finally
{
CloseConnection();
}
}
return 0;
}
public void Delete(string table, string selector)
{
string querry = $"DELETE FROM `{table}` WHERE {selector};";
if (this.OpenConnection())
{
MySqlCommand deleteCommand = new MySqlCommand();
deleteCommand.CommandText = querry;
deleteCommand.Connection = connection;
deleteCommand.ExecuteNonQuery();
CloseConnection();
}
}
}
}