2022-10-14 16:08:54 +08:00
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
{
/// <summary>
/// Database 介面
/// </summary>
public interface IDatabaseHelper
{
/// <summary>
/// 取得連線
/// </summary>
/// <returns></returns>
string GetMSSqlConnectionString ( ) ;
string GetMySqlConnectionString ( ) ;
}
public class DatabaseHelper : IDatabaseHelper
{
private MySqlDBConfig _mySqlDBConfig ;
private MSSqlDBConfig _msSqlDBConfig ;
public DatabaseHelper ( IOptions < DBConfig > 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 ) ;
2023-09-22 18:38:54 +08:00
var portStr = ! string . IsNullOrEmpty ( _msSqlDBConfig . Port ) ? ed . AESDecrypt ( _msSqlDBConfig . Port ) : string . Empty ;
2022-10-14 16:08:54 +08:00
2023-09-22 18:38:54 +08:00
string source = ! string . IsNullOrEmpty ( portStr ) ? string . Join ( "," , serverStr , portStr ) : serverStr ;
var connStr = $"data source={source};initial catalog={databaseStr};User ID={rootStr};Password={passwordStr}" ;
2022-10-14 16:08:54 +08:00
//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
2022-12-22 10:11:31 +08:00
//var portStr = ed.AESEncrypt("33306"); // 33306
//var databaseStr = ed.AESEncrypt("bims_mitsubishi"); // bims_wsp: IgYBsgG2VLKKxFb64j7LOA== ; bims_mitsubishi
2022-10-14 16:08:54 +08:00
//var rootStr = ed.AESEncrypt("bims"); // bims
//var passwordStr = ed.AESEncrypt("mjmdev_BIMS2022"); // mjmdev_BIMS2022
2023-09-07 16:16:58 +08:00
var connStr = $"server={serverStr};port={portStr};database={databaseStr};user={rootStr};password={passwordStr};charset=utf8;Allow User Variables=True;SslMode=None;" ;
2022-10-14 16:08:54 +08:00
//var conn = new MySqlConnection(connStr);
return connStr ;
}
}
}