From 00176a2d833a568966adb84b219e2d0731db49ca Mon Sep 17 00:00:00 2001 From: Kuro Date: Fri, 7 Mar 2025 23:36:20 +0100 Subject: [PATCH] =?UTF-8?q?erste=20anpassungen=20f=C3=BCr=20neue=20impemen?= =?UTF-8?q?tierte=20connection=20der=20verschiedenen=20sql=20server?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProgrammList.sln | 6 +- ProgrammList/ConfigManager/ConfigManager.cs | 32 ++++-- ProgrammList/Program.cs | 2 +- ProgrammList/Properties/Resources.Designer.cs | 63 +++++++++++ ProgrammList/Properties/Resources.resx | 101 ++++++++++++++++++ ProgrammList/list_creation/ListPrograms.cs | 10 +- ProgrammList/sql/Mssql.cs | 48 ++++++--- ProgrammList/sql/MySql.cs | 24 +++-- ProgrammList/sql/SqlBase.cs | 3 +- ProgrammList/sql/SqlBaseAbstract.cs | 86 --------------- ProgrammList/sql/Sqlite.cs | 36 ++++++- 11 files changed, 285 insertions(+), 126 deletions(-) create mode 100644 ProgrammList/Properties/Resources.Designer.cs create mode 100644 ProgrammList/Properties/Resources.resx delete mode 100644 ProgrammList/sql/SqlBaseAbstract.cs diff --git a/ProgrammList.sln b/ProgrammList.sln index 1c91ef8..afd4833 100644 --- a/ProgrammList.sln +++ b/ProgrammList.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 -VisualStudioVersion = 17.12.35527.113 d17.12 +VisualStudioVersion = 17.12.35527.113 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProgrammList", "ProgrammList\ProgrammList.csproj", "{06D431CD-A938-4DFD-8D77-4C612FDAD914}" EndProject @@ -15,8 +15,8 @@ Global Release|x64 = Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {06D431CD-A938-4DFD-8D77-4C612FDAD914}.Debug|Any CPU.ActiveCfg = Release|Any CPU - {06D431CD-A938-4DFD-8D77-4C612FDAD914}.Debug|Any CPU.Build.0 = Release|Any CPU + {06D431CD-A938-4DFD-8D77-4C612FDAD914}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {06D431CD-A938-4DFD-8D77-4C612FDAD914}.Debug|Any CPU.Build.0 = Debug|Any CPU {06D431CD-A938-4DFD-8D77-4C612FDAD914}.Debug|x64.ActiveCfg = Debug|x64 {06D431CD-A938-4DFD-8D77-4C612FDAD914}.Debug|x64.Build.0 = Debug|x64 {06D431CD-A938-4DFD-8D77-4C612FDAD914}.PrgmList|Any CPU.ActiveCfg = PrgmList|Any CPU diff --git a/ProgrammList/ConfigManager/ConfigManager.cs b/ProgrammList/ConfigManager/ConfigManager.cs index f8a9fa4..198815a 100644 --- a/ProgrammList/ConfigManager/ConfigManager.cs +++ b/ProgrammList/ConfigManager/ConfigManager.cs @@ -1,17 +1,35 @@ using System.Configuration; namespace ProgrammList.ConfigManager { - public class ConfigManager { + public static class PrmListConfigManager { + public static string GetSetting(string key) { - return ConfigurationManager.AppSettings[key]; + try { + ExeConfigurationFileMap configMap = new ExeConfigurationFileMap(); + Console.WriteLine("Loading " + Directory.GetCurrentDirectory() + "\\app.conf"); + configMap.ExeConfigFilename = Directory.GetCurrentDirectory() + "\\app.conf"; + Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None); + return config.AppSettings.Settings[key].Value; + } + catch (Exception e) { + Console.WriteLine("Error on Key " + key + ": " + e.ToString()); + Console.WriteLine(e.ToString()); + } + return "13"; } public static void SetSetting(string key, string value) { - Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); - configuration.AppSettings.Settings[key].Value = value; - configuration.Save(ConfigurationSaveMode.Full, true); - ConfigurationManager.RefreshSection("appSettings"); - + try { + ExeConfigurationFileMap configMap = new ExeConfigurationFileMap(); + Console.WriteLine("Loading " + Directory.GetCurrentDirectory() + "\\app.conf"); + configMap.ExeConfigFilename = Directory.GetCurrentDirectory() + "\\app.conf"; + Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None); + config.AppSettings.Settings["key"].Value = value; + } + catch (Exception e) { + Console.WriteLine("Error on Key " + key + ": " + e.ToString()); + Console.WriteLine(e.ToString()); + } } } } \ No newline at end of file diff --git a/ProgrammList/Program.cs b/ProgrammList/Program.cs index e936474..7cd86f1 100644 --- a/ProgrammList/Program.cs +++ b/ProgrammList/Program.cs @@ -4,7 +4,7 @@ using ProgrammList.ListPrograms; class Program { public static void Main(string[] args) { - string dbType = ConfigManager.GetSetting("DB_Type"); + string dbType = PrmListConfigManager.GetSetting("DB_Type"); ListPrograms list = new ListPrograms(dbType); Console.WriteLine("Deleting old data"); diff --git a/ProgrammList/Properties/Resources.Designer.cs b/ProgrammList/Properties/Resources.Designer.cs new file mode 100644 index 0000000..084a270 --- /dev/null +++ b/ProgrammList/Properties/Resources.Designer.cs @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ProgrammList.Properties { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ProgrammList.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + } +} diff --git a/ProgrammList/Properties/Resources.resx b/ProgrammList/Properties/Resources.resx new file mode 100644 index 0000000..4fdb1b6 --- /dev/null +++ b/ProgrammList/Properties/Resources.resx @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ProgrammList/list_creation/ListPrograms.cs b/ProgrammList/list_creation/ListPrograms.cs index b17d861..db2a5e6 100644 --- a/ProgrammList/list_creation/ListPrograms.cs +++ b/ProgrammList/list_creation/ListPrograms.cs @@ -9,7 +9,7 @@ namespace ProgrammList.ListPrograms { string[] keyvaluenames = { "DisplayName", "DisplayVersion", "InstallDate" }; private SqlBase sql = null; - internal ListPrograms(string sqlType) { + internal ListPrograms(string sqlType, params string[] filename) { if (sqlType == null) { Console.WriteLine("SQL Database not defined in app.conf, allowed types:"); Console.WriteLine("MYSQL"); @@ -25,8 +25,12 @@ namespace ProgrammList.ListPrograms { else if (sqlType.Equals("MSSQL", StringComparison.OrdinalIgnoreCase)) { sql = new Mssql(); } - else if (sqlType.Equals("SQLITE", StringComparison.OrdinalIgnoreCase)) { - sql = new Sqlite(prgm_path); + else if (sqlType.Equals("SQLITE", StringComparison.OrdinalIgnoreCase) && filename != null) { + sql = new Sqlite(prgm_path, filename[0]); + } + else { + // Default sqlite im gleichen ordner + sql = new Sqlite(prgm_path, "sqllite.db"); } } diff --git a/ProgrammList/sql/Mssql.cs b/ProgrammList/sql/Mssql.cs index 72e5a0d..4d2f2ea 100644 --- a/ProgrammList/sql/Mssql.cs +++ b/ProgrammList/sql/Mssql.cs @@ -3,7 +3,13 @@ using Microsoft.Data.SqlClient; namespace ProgrammList.sql { - public class Mssql : SqlBaseAbstract { + public class Mssql : SqlBase { + + public string[] valuenames = { "PCID", "DisplayName", "DisplayVersion", "InstallDate", "update_date", "APP_Architecture" }; + public string connstring = null; + string constring = null; + + SqlConnection mssqlcon = null; //private static Dbconnection instance; public Mssql() { @@ -15,12 +21,22 @@ namespace ProgrammList.sql { TrustServerCertificate = true }; - connstring = builder.ToString(); + constring = builder.ToString(); + } + + + + public void Open() { + mssqlcon.Open(); + } + + public void Close() { + mssqlcon.Close(); } public void GetAllData() { - Open(DB.MSSQL); - var command = sqlcon.CreateCommand(); + Open(); + var command = mssqlcon.CreateCommand(); command.CommandText = @"SELECT * FROM list"; using (var reader = command.ExecuteReader()) { @@ -31,8 +47,8 @@ namespace ProgrammList.sql { Close(); } public bool GetSingleLine(string pcid, string program, string version) { - Open(DB.MSSQL); - var command = sqlcon.CreateCommand(); + Open(); + var command = mssqlcon.CreateCommand(); command.CommandText = @"SELECT * FROM list where PCID like " + pcid + " and DisplayName like " + program + " and DisplayVersion like " + version + ";"; @@ -43,8 +59,8 @@ namespace ProgrammList.sql { } public void CheckTableExists() { - Open(DB.MSSQL); - var command = sqlcon.CreateCommand(); + Open(); + var command = mssqlcon.CreateCommand(); command.CommandText = "select case when exists (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'prgmlist' AND TABLE_NAME = 'list') then 1 else 0 end"; var returncode = command.ExecuteScalar(); if (returncode != null) { @@ -71,8 +87,8 @@ namespace ProgrammList.sql { public void InsertData(Dictionary valuesqlCommand) { - Open(DB.MSSQL); - var transaction = sqlcon.BeginTransaction(); + Open(); + var transaction = mssqlcon.BeginTransaction(); string result = ""; for (int i = 0; i < valuenames.Length; i++) { @@ -88,7 +104,7 @@ namespace ProgrammList.sql { string sqlCommand = "INSERT INTO list(" + cols + ")" + "VALUES(" + result + ")"; - var command = new SqlCommand(sqlCommand, sqlcon, transaction); + var command = new SqlCommand(sqlCommand, mssqlcon, transaction); command.ExecuteNonQuery(); transaction.Commit(); Console.WriteLine(sqlCommand); @@ -105,8 +121,8 @@ namespace ProgrammList.sql { } public void DeleteOldData(string hostname) { - Open(DB.MSSQL); - var command = sqlcon.CreateCommand(); + Open(); + var command = mssqlcon.CreateCommand(); string sqlCommand = @"delete from list where PCID = '" + hostname + "';"; command.CommandText = sqlCommand; command.ExecuteReader(); @@ -114,8 +130,8 @@ namespace ProgrammList.sql { } public void UpdateData(Dictionary value) { - Open(DB.MSSQL); - var transaction = sqlcon.BeginTransaction(); + Open(); + var transaction = mssqlcon.BeginTransaction(); string sqlCommand = @"Update list "; string result = "set "; @@ -133,7 +149,7 @@ namespace ProgrammList.sql { " and DisplayVersion like " + value.GetValueOrDefault("DisplayVersion"); - var command = new SqlCommand(sqlCommand, sqlcon, transaction); + var command = new SqlCommand(sqlCommand, mssqlcon, transaction); for (int i = 0; i < valuenames.Length; i++) { if (valuenames[i] != "PCID") { command.Parameters.AddWithValue("$" + valuenames[i], value.GetValueOrDefault(valuenames[i])); diff --git a/ProgrammList/sql/MySql.cs b/ProgrammList/sql/MySql.cs index 2cc5a57..3439983 100644 --- a/ProgrammList/sql/MySql.cs +++ b/ProgrammList/sql/MySql.cs @@ -3,11 +3,14 @@ namespace ProgrammList.sql { - internal class Mysql : SqlBaseAbstract { + internal class Mysql { + public string[] valuenames = { "PCID", "DisplayName", "DisplayVersion", "InstallDate", "update_date", "APP_Architecture" }; + public string connstring = null; + public MySqlConnection mysqlcon = null; public Boolean GetSingleLine(string pcid, string program, string version) { - Open(DB.MYSQL); + Open(); var command = mysqlcon.CreateCommand(); command.CommandText = @"SELECT * FROM list where PCID like " + pcid + " and DisplayName like " @@ -18,8 +21,17 @@ namespace ProgrammList.sql { return result; } + + public void Open() { + mysqlcon.Open(); + } + + public void Close() { + mysqlcon.Close(); + } + public void CheckTableExists() { - Open(DB.MYSQL); + Open(); var command = mysqlcon.CreateCommand(); command.CommandText = @"SHOW TABLES LIKE 'list';"; var name = command.ExecuteScalar(); @@ -35,7 +47,7 @@ namespace ProgrammList.sql { public void InsertData(Dictionary valuesqlCommand) { - Open(DB.MYSQL); + Open(); var transaction = mysqlcon.BeginTransaction(); string result = ""; @@ -69,7 +81,7 @@ namespace ProgrammList.sql { } public void UpdateData(Dictionary value) { - Open(DB.MYSQL); + Open(); var transaction = mysqlcon.BeginTransaction(); string sqlCommand = @"Update list "; @@ -102,7 +114,7 @@ namespace ProgrammList.sql { public void DeleteOldData(string hostname) { - Open(DB.MYSQL); + Open(); var command = mysqlcon.CreateCommand(); string sqlCommand = @"delete from list where PCID = '" + hostname + "';"; command.CommandText = sqlCommand; diff --git a/ProgrammList/sql/SqlBase.cs b/ProgrammList/sql/SqlBase.cs index 296bae4..e876a11 100644 --- a/ProgrammList/sql/SqlBase.cs +++ b/ProgrammList/sql/SqlBase.cs @@ -2,7 +2,8 @@ interface SqlBase { - + internal void Open(); + internal void Close(); internal bool GetSingleLine(string pcid, string program, string version); internal void CheckTableExists(); diff --git a/ProgrammList/sql/SqlBaseAbstract.cs b/ProgrammList/sql/SqlBaseAbstract.cs deleted file mode 100644 index 1c5b570..0000000 --- a/ProgrammList/sql/SqlBaseAbstract.cs +++ /dev/null @@ -1,86 +0,0 @@ -using Microsoft.Data.SqlClient; -using Microsoft.Data.Sqlite; -using MySql.Data.MySqlClient; - -namespace ProgrammList.sql { - - public abstract class SqlBaseAbstract : SqlBase { - - - public string[] valuenames = { "PCID", "DisplayName", "DisplayVersion", "InstallDate", "update_date", "APP_Architecture" }; - public string connstring = null; - public MySqlConnection mysqlcon = null; - public SqlConnection sqlcon = null; - public SqliteConnection sqlitecon = null; - - public void Close() { - if (mysqlcon != null) { - mysqlcon.Close(); - } - else if (sqlcon != null) { - sqlcon.Close(); - } - else if (sqlitecon != null) { - sqlitecon.Close(); - } - } - - - public void Open(DB type) { - switch (type) { - case DB.MYSQL: - case DB.MARIADB: - mysqlcon = new MySqlConnection(connstring); - try { - mysqlcon.Open(); - } - catch (Exception ex) { - Console.Write(ex.ToString()); - } - break; - case DB.MSSQL: - sqlcon = new SqlConnection(connstring); - try { - mysqlcon.Open(); - } - catch (Exception ex) { - Console.Write(ex.ToString()); - } - break; - case DB.SQLITE: - sqlitecon = new SqliteConnection(connstring); - try { - mysqlcon.Open(); - } - catch (Exception ex) { - Console.Write(ex.ToString()); - } - break; - } - } - - bool SqlBase.GetSingleLine(string pcid, string program, string version) { - throw new NotImplementedException(); - } - - void SqlBase.CheckTableExists() { - throw new NotImplementedException(); - } - - void SqlBase.InsertData(Dictionary valuesqlCommand) { - throw new NotImplementedException(); - } - - void SqlBase.InsertOrUpdateData(Dictionary value) { - throw new NotImplementedException(); - } - - void SqlBase.DeleteOldData(string hostname) { - throw new NotImplementedException(); - } - - void SqlBase.UpdateData(Dictionary value) { - throw new NotImplementedException(); - } - } -} diff --git a/ProgrammList/sql/Sqlite.cs b/ProgrammList/sql/Sqlite.cs index 539d24f..ccb4c47 100644 --- a/ProgrammList/sql/Sqlite.cs +++ b/ProgrammList/sql/Sqlite.cs @@ -1,13 +1,43 @@ using Microsoft.Data.Sqlite; -using System.Configuration; namespace ProgrammList.sql { - public class Sqlite : SqlBaseAbstract { + public class Sqlite : SqlBase { string filename; + SqliteConnection sqlitecon; + + public string[] valuenames = { "PCID", "DisplayName", "DisplayVersion", "InstallDate", "update_date", "APP_Architecture" }; + public string connstring = null; + + public Sqlite(string prm_path, string filename) { + sqlitecon = new SqliteConnection("Data Source=" + prm_path + filename); + } + + public void Open() { + sqlitecon.Open(); + } + + public void Close() { + sqlitecon.Close(); + } + + + public void Open(DB type) { + sqlitecon = new SqliteConnection(connstring); + sqlitecon.Open(); + } + public Sqlite(string prm_path) { - sqlitecon = new SqliteConnection("Data Source=" + prm_path + ConfigurationManager.AppSettings["filename"]); + string setting = ConfigManager.PrmListConfigManager.GetSetting("filename"); + if (setting != null && setting != "") { + Console.WriteLine("using db filename " + setting); + sqlitecon = new SqliteConnection("Data Source=" + prm_path + setting); + } + else { + Console.WriteLine("reverting to default db, sqlite filename sqlite.db"); + sqlitecon = new SqliteConnection("Data Source=" + prm_path + "sqlite.db"); + } sqlitecon.Open(); }