FIC_Solar/SolarPower/Helper/DatabaseHelper.cs
2021-06-23 10:59:10 +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 databaseStr = ed.AESDecrypt(dbConfig.Database);
var rootStr = ed.AESDecrypt(dbConfig.Root);
var passwordStr = ed.AESDecrypt(dbConfig.Password);
//var connStr = $"server={serverStr};database={databaseStr};user={rootStr};password={passwordStr};charset=utf8;";
var connStr = @"server=127.0.0.1;port=3306;database=solar_power;user=root;password=000000;charset=utf8;";
this._connectionString = connStr;
}
public IDbConnection GetConnection()
{
var conn = new MySqlConnection(this._connectionString);
return conn;
}
}
}