commit 4c261d035bb065eda6995bc9f8afaf6a819c5858 Author: JacobTech Date: Tue Feb 27 17:42:25 2024 -0500 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2fd0117 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ +.idea/ +discord.xml \ No newline at end of file diff --git a/MySQL_Manager.sln b/MySQL_Manager.sln new file mode 100644 index 0000000..814c9ca --- /dev/null +++ b/MySQL_Manager.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MySQL_Manager", "MySQL_Manager\MySQL_Manager.csproj", "{0A6AA01B-91AE-4BBB-90E5-C15F70197601}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0A6AA01B-91AE-4BBB-90E5-C15F70197601}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0A6AA01B-91AE-4BBB-90E5-C15F70197601}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0A6AA01B-91AE-4BBB-90E5-C15F70197601}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0A6AA01B-91AE-4BBB-90E5-C15F70197601}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/MySQL_Manager/Database.cs b/MySQL_Manager/Database.cs new file mode 100644 index 0000000..f3512ab --- /dev/null +++ b/MySQL_Manager/Database.cs @@ -0,0 +1,53 @@ +using MySqlConnector; + +namespace MySQL_Manager; + +public class Database +{ + private string conection; + + public Database(string ConnectionString) + { + conection = ConnectionString; + } + + public void ExecuteNonQuery(string command) + { + try + { + MySqlConnection conn = new MySqlConnection(conection); + conn.Open(); + MySqlCommand cmd = new MySqlCommand(command, conn); + _ = cmd.ExecuteNonQuery(); + conn.Close(); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } + } + + public TReturn Read(string command) + { + try + { + MySqlConnection conn = new MySqlConnection(conection); + conn.Open(); + MySqlCommand cmd = new MySqlCommand(command, conn); + object? temp = cmd.ExecuteScalar(); + if (temp is DBNull || temp is null) + { + return default!; + } + if (typeof(TReturn).IsEnum) return (TReturn?)Enum.Parse(typeof(TReturn), temp.ToString()!)!; + if (typeof(TReturn).IsNullableEnum()) return (TReturn?)Enum.Parse(Nullable.GetUnderlyingType(typeof(TReturn))!, temp.ToString()!)!; + return (TReturn?)temp!; + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } + } +} \ No newline at end of file diff --git a/MySQL_Manager/EXT.cs b/MySQL_Manager/EXT.cs new file mode 100644 index 0000000..6cbcb4e --- /dev/null +++ b/MySQL_Manager/EXT.cs @@ -0,0 +1,10 @@ +namespace MySQL_Manager; + +internal static class EXT +{ + internal static bool IsNullableEnum(this Type t) + { + Type u = Nullable.GetUnderlyingType(t)!; + return (u != null) && u.IsEnum; + } +} \ No newline at end of file diff --git a/MySQL_Manager/MySQL_Manager.csproj b/MySQL_Manager/MySQL_Manager.csproj new file mode 100644 index 0000000..3edacf9 --- /dev/null +++ b/MySQL_Manager/MySQL_Manager.csproj @@ -0,0 +1,20 @@ + + + + net8.0 + enable + enable + MySQL Manager + A Database class to handle database interactions in MySQL for school Projects + + + + + + + + + + + +