75 lines
2.6 KiB
C#
75 lines
2.6 KiB
C#
|
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 = ed.AESDecrypt(_msSqlDBConfig.Port);
|
|||
|
|
|||
|
var connStr = $"data source={serverStr},{portStr};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_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;";
|
|||
|
|
|||
|
//var conn = new MySqlConnection(connStr);
|
|||
|
return connStr;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|