/// /// Copyright (c) 2017-2022 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Config { public static class Utils { /// /// Extract and return a DB server from a MySQL connection string. /// public static string GetDBServer(string connectionString) { return GetFromConnectionString(connectionString, new string[] { "SERVER=" }); } /// /// Extract and return a DB name from a MySQL connection string. /// public static string GetDBName(string connectionString) { return GetFromConnectionString(connectionString, new string[] { "DATABASE=" }); } /// /// Extract and return a username from a MySQL connection string /// public static string GetDBUser(string connectionString) { return GetFromConnectionString(connectionString, new string[] { "USER=", "UID=" }); } /// /// Extract and return a password from a MySQL connection string /// public static string GetDBPassword(string connectionString) { return GetFromConnectionString(connectionString, new string[] { "PASSWORD=", "PWD=" }); } /// /// Extract and return an element of a MySQL connection string /// (a host, a database, a user name or a password). /// Return an empty string on any error. /// public static string GetFromConnectionString(string connectionString, string[] patterns) { foreach (var pattern in patterns) { int startIx = connectionString.IndexOf(pattern); if (startIx >= 0) { /// pattern found, extract the subsequent element startIx += pattern.Length; int endIx = connectionString.IndexOf(';', startIx); return (endIx > 0) ? connectionString.Substring(startIx, endIx - startIx) : string.Empty; } } /// pattern NOT found return string.Empty; } } }