ibms-dome/Repository/Helper/DatabaseHelper.cs

76 lines
2.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
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=Required;"; // 2024.04.09 SSLmode改成Required改善重開機無法自動連上MYSQL的問題
//var conn = new MySqlConnection(connStr);
return connStr;
}
}
}