using Microsoft.Extensions.Options; using MySql.Data.MySqlClient; using Repository.Models; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Threading.Tasks; namespace Repository.Helper { /// /// Database 介面 /// public interface IDatabaseHelper { /// /// 取得連線 /// /// string GetMSSqlConnectionString(); string GetMySqlConnectionString(); } public class DatabaseHelper : IDatabaseHelper { private MySqlDBConfig _mySqlDBConfig; private MSSqlDBConfig _msSqlDBConfig; public DatabaseHelper(IOptions dbConfig) { _mySqlDBConfig = dbConfig.Value.MySqlDBConfig; _msSqlDBConfig = dbConfig.Value.MSSqlDBConfig; } public string GetMSSqlConnectionString() { EDFunction ed = new EDFunction(); var serverStr = ed.AESDecrypt(_msSqlDBConfig.Server); var databaseStr = ed.AESDecrypt(_msSqlDBConfig.Database); var rootStr = ed.AESDecrypt(_msSqlDBConfig.Root); var passwordStr = ed.AESDecrypt(_msSqlDBConfig.Password); var portStr = !string.IsNullOrEmpty(_msSqlDBConfig.Port) ? ed.AESDecrypt(_msSqlDBConfig.Port) : string.Empty; string source = !string.IsNullOrEmpty(portStr) ? string.Join(",", serverStr, portStr) : serverStr; var connStr = $"data source={source};initial catalog={databaseStr};User ID={rootStr};Password={passwordStr}"; //var conn = new SqlConnection(connStr); return connStr; } public string GetMySqlConnectionString() { EDFunction ed = new EDFunction(); var serverStr = ed.AESDecrypt(_mySqlDBConfig.Server); //0.201 var portStr = ed.AESDecrypt(_mySqlDBConfig.Port); // var databaseStr = ed.AESDecrypt(_mySqlDBConfig.Database); // var rootStr = ed.AESDecrypt(_mySqlDBConfig.Root); // var passwordStr = ed.AESDecrypt(_mySqlDBConfig.Password); // //var serverStr = ed.AESEncrypt("192.168.0.201"); //192.168.0.201 //var portStr = ed.AESEncrypt("33306"); // 33306 //var databaseStr = ed.AESEncrypt("bims_mitsubishi"); // bims_wsp: IgYBsgG2VLKKxFb64j7LOA== ; bims_mitsubishi //var rootStr = ed.AESEncrypt("bims"); // bims //var passwordStr = ed.AESEncrypt("mjmdev_BIMS2022"); // mjmdev_BIMS2022 var connStr = $"server={serverStr};port={portStr};database={databaseStr};user={rootStr};password={passwordStr};charset=utf8;Allow User Variables=True;SslMode=None;"; //var conn = new MySqlConnection(connStr); return connStr; } } }