using MySql.Data.MySqlClient;
using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Library.Common.Architecture;
namespace Milimoe.FunGame.Server.Utility.DataUtility
{
public class ConnectProperties
{
public static string Name { get; set; } = "";
public static string DataSource { get; set; } = "";
public static string Port { get; set; } = "";
public static string DataBase { get; set; } = "";
public static string User { get; set; } = "";
public static string Password { get; set; } = "";
///
/// 读取MySQL服务器配置文件
///
///
public static string GetConnectProperties()
{
if (Name == "" && DataSource == "" && Port == "" && DataBase == "" && User == "" && Password == "")
{
if (INIHelper.ExistINIFile())
{
DataSource = INIHelper.ReadINI("MySQL", "DBServer");
Port = INIHelper.ReadINI("MySQL", "DBPort");
DataBase = INIHelper.ReadINI("MySQL", "DBName");
User = INIHelper.ReadINI("MySQL", "DBUser");
Password = INIHelper.ReadINI("MySQL", "DBPassword");
return "data source = " + DataSource + "; port = " + Port + "; database = " + DataBase + "; user = " + User + "; password = " + Password + "; charset = utf8mb4;";
}
else ServerHelper.Error(new MySQLConfigException());
}
return "data source = " + DataSource + "; port = " + Port + "; database = " + DataBase + "; user = " + User + "; password = " + Password + "; charset = utf8mb4;";
}
}
public class MySQLConnection
{
public MySqlConnection? Connection
{
get
{
return _Connection;
}
}
public SQLServerInfo ServerInfo
{
get
{
return _ServerInfo ?? SQLServerInfo.Create();
}
}
private MySqlConnection? _Connection;
private SQLServerInfo? _ServerInfo;
///
/// 创建SQL连接
///
///
public MySQLConnection(out SQLServerInfo? serverInfo)
{
_Connection = Connect(out serverInfo);
}
///
/// 关闭连接
///
public void Close()
{
if (_Connection != null && _Connection.State == System.Data.ConnectionState.Open)
{
_Connection.Close();
}
_Connection = null;
}
///
/// 连接MySQL服务器
///
/// 服务器信息
/// 连接对象
/// MySQL服务启动失败:无法找到MySQL配置文件
private MySqlConnection? Connect(out SQLServerInfo? serverInfo)
{
try
{
string _GetConnection = ConnectProperties.GetConnectProperties();
if (_GetConnection != null)
{
string[] DataSetting = _GetConnection.Split(";");
if (DataSetting.Length > 1 && DataSetting[0].Length > 14 && DataSetting[1].Length > 8)
{
ServerHelper.WriteLine("Connect -> MySQL://" + DataSetting[0][14..] + ":" + DataSetting[1][8..]);
}
_Connection = new MySqlConnection(_GetConnection);
_Connection.Open();
if (_Connection.State == System.Data.ConnectionState.Open)
{
_ServerInfo = SQLServerInfo.Create(ConnectProperties.Name, ConnectProperties.DataSource, ConnectProperties.Port, ConnectProperties.DataBase, ConnectProperties.User, ConnectProperties.Password);
ServerHelper.WriteLine("Connected: MySQL服务器连接成功");
}
}
else
{
throw new MySQLConfigException();
}
}
catch (Exception e)
{
ServerHelper.Error(e);
}
serverInfo = _ServerInfo;
return _Connection;
}
}
}