FIC_Solar/SolarPower/Helper/DatabaseHelper.cs
Kai 830937f967 1. bug fix
2. 背景執行 時間設定 拉出至config
2021-07-14 11:53:37 +08:00

52 lines
1.4 KiB
C#

using Microsoft.Extensions.Options;
using MySql.Data.MySqlClient;
using SolarPower.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
namespace SolarPower.Helper
{
/// <summary>
/// Database 介面
/// </summary>
public interface IDatabaseHelper
{
/// <summary>
/// 取得連線
/// </summary>
/// <returns></returns>
IDbConnection GetConnection();
}
public class DatabaseHelper : IDatabaseHelper
{
private readonly string _connectionString;
public DatabaseHelper(DBConfig dbConfig)
{
EDFunction ed = new EDFunction();
var serverStr = ed.AESDecrypt(dbConfig.Server);
var portStr = ed.AESDecrypt(dbConfig.Port);
var databaseStr = ed.AESDecrypt(dbConfig.Database);
var rootStr = ed.AESDecrypt(dbConfig.Root);
var passwordStr = ed.AESDecrypt(dbConfig.Password);
var connStr = $"server={serverStr};port={portStr};database={databaseStr};user={rootStr};password={passwordStr};charset=utf8;";
this._connectionString = connStr;
}
public IDbConnection GetConnection()
{
var conn = new MySqlConnection(this._connectionString);
return conn;
}
}
}