mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-23 04:29:38 +08:00
添加SQLHelper,SQL处理器,验证账号密码
This commit is contained in:
parent
bf6b50ba9f
commit
49b96979f2
@ -5,7 +5,6 @@ using Milimoe.FunGame.Core.Library.Constant;
|
|||||||
using Milimoe.FunGame.Server.Model;
|
using Milimoe.FunGame.Server.Model;
|
||||||
using Milimoe.FunGame.Server.Others;
|
using Milimoe.FunGame.Server.Others;
|
||||||
using Milimoe.FunGame.Server.Utility;
|
using Milimoe.FunGame.Server.Utility;
|
||||||
using Milimoe.FunGame.Server.Utility.DataUtility;
|
|
||||||
|
|
||||||
Console.Title = Config.SERVER_NAME;
|
Console.Title = Config.SERVER_NAME;
|
||||||
Console.WriteLine(FunGameInfo.GetInfo((FunGameInfo.FunGame)Config.FunGameType));
|
Console.WriteLine(FunGameInfo.GetInfo((FunGameInfo.FunGame)Config.FunGameType));
|
||||||
@ -64,7 +63,6 @@ void StartServer()
|
|||||||
ServerHelper.WriteLine("未检测到配置文件,将自动创建配置文件 . . .");
|
ServerHelper.WriteLine("未检测到配置文件,将自动创建配置文件 . . .");
|
||||||
INIHelper.Init((FunGameInfo.FunGame)Config.FunGameType);
|
INIHelper.Init((FunGameInfo.FunGame)Config.FunGameType);
|
||||||
ServerHelper.WriteLine("配置文件FunGame.ini创建成功,请修改该配置文件,然后重启服务器。");
|
ServerHelper.WriteLine("配置文件FunGame.ini创建成功,请修改该配置文件,然后重启服务器。");
|
||||||
ServerHelper.WriteLine("请输入 help 来获取帮助,输入 quit 关闭服务器。");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -72,21 +70,19 @@ void StartServer()
|
|||||||
ServerHelper.GetServerSettings();
|
ServerHelper.GetServerSettings();
|
||||||
Console.Title = Config.SERVER_NAME + " - FunGame Server Port: " + Config.SERVER_PORT;
|
Console.Title = Config.SERVER_NAME + " - FunGame Server Port: " + Config.SERVER_PORT;
|
||||||
}
|
}
|
||||||
|
ServerHelper.WriteLine("请输入 help 来获取帮助,输入 quit 关闭服务器。");
|
||||||
|
|
||||||
MySQLConnection.Close();
|
// 测试MySQL服务器连接
|
||||||
|
if (TestSQLConnection() != SQLResult.Success)
|
||||||
// 连接MySQL服务器
|
|
||||||
if (!MySQLConnection.Connect(out _))
|
|
||||||
{
|
{
|
||||||
Running = false;
|
Running = false;
|
||||||
throw new ServerErrorException();
|
throw new SQLQueryException();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建监听
|
// 创建监听
|
||||||
ListeningSocket = ServerSocket.StartListening();
|
ListeningSocket = ServerSocket.StartListening();
|
||||||
|
|
||||||
// 开始监听连接
|
// 开始监听连接
|
||||||
//ServerSocket.Listen(Config.MAX_PLAYERS);
|
|
||||||
ServerHelper.WriteLine("Listen -> " + Config.SERVER_PORT);
|
ServerHelper.WriteLine("Listen -> " + Config.SERVER_PORT);
|
||||||
ServerHelper.WriteLine("服务器启动成功,开始监听 . . .");
|
ServerHelper.WriteLine("服务器启动成功,开始监听 . . .");
|
||||||
|
|
||||||
@ -127,7 +123,7 @@ void StartServer()
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
if (e.Message.Equals("服务器遇到问题需要关闭,请重新启动服务器!"))
|
if (e.Message.Equals(new ServerErrorException().Message))
|
||||||
{
|
{
|
||||||
if (ListeningSocket != null)
|
if (ListeningSocket != null)
|
||||||
{
|
{
|
||||||
@ -182,4 +178,13 @@ bool Send(ClientSocket socket)
|
|||||||
else
|
else
|
||||||
ServerHelper.WriteLine("无法传输数据,与客户端的连接可能丢失。");
|
ServerHelper.WriteLine("无法传输数据,与客户端的连接可能丢失。");
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
SQLResult TestSQLConnection()
|
||||||
|
{
|
||||||
|
SQLResult TestResult = SQLResult.Success;
|
||||||
|
MySQLHelper SQLHelper = MySQLHelper.GetHelper();
|
||||||
|
SQLHelper.Script = SQLConstant.Insert_ServerLoginLogs(Config.SERVER_NAME, Config.SERVER_KEY);
|
||||||
|
SQLHelper.Execute(out TestResult);
|
||||||
|
return TestResult;
|
||||||
}
|
}
|
@ -5,6 +5,7 @@ using Milimoe.FunGame.Core.Library.Constant;
|
|||||||
using Milimoe.FunGame.Core.Library.Server;
|
using Milimoe.FunGame.Core.Library.Server;
|
||||||
using Milimoe.FunGame.Server.Others;
|
using Milimoe.FunGame.Server.Others;
|
||||||
using Milimoe.FunGame.Server.Utility;
|
using Milimoe.FunGame.Server.Utility;
|
||||||
|
using System.Data;
|
||||||
|
|
||||||
namespace Milimoe.FunGame.Server.Model
|
namespace Milimoe.FunGame.Server.Model
|
||||||
{
|
{
|
||||||
@ -26,6 +27,7 @@ namespace Milimoe.FunGame.Server.Model
|
|||||||
private string UserName = "";
|
private string UserName = "";
|
||||||
private string Password = "";
|
private string Password = "";
|
||||||
private int FailedTimes = 0; // 超过一定次数断开连接
|
private int FailedTimes = 0; // 超过一定次数断开连接
|
||||||
|
private MySQLHelper SQLHelper = MySQLHelper.GetHelper();
|
||||||
|
|
||||||
public ServerModel(ClientSocket socket, bool running)
|
public ServerModel(ClientSocket socket, bool running)
|
||||||
{
|
{
|
||||||
@ -70,8 +72,11 @@ namespace Milimoe.FunGame.Server.Model
|
|||||||
if (username != null && password != null)
|
if (username != null && password != null)
|
||||||
{
|
{
|
||||||
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(type) + "] UserName: " + username);
|
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(type) + "] UserName: " + username);
|
||||||
if (username == "test" && password == "123456".Encrypt("test"))
|
SQLHelper.Script = $"{SQLConstant.Select_Users} {SQLConstant.Command_Where} Username = '{username}' And Password = '{password}'";
|
||||||
|
SQLHelper.ExecuteDataSet(out SQLResult result);
|
||||||
|
if (result == SQLResult.Success && SQLHelper.UpdateRows > 0)
|
||||||
{
|
{
|
||||||
|
DataRow UserRow = SQLHelper.DataSet.Tables[0].Rows[0];
|
||||||
if (autokey != null && autokey.Trim() != "")
|
if (autokey != null && autokey.Trim() != "")
|
||||||
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(type) + "] AutoKey: 已确认");
|
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(type) + "] AutoKey: 已确认");
|
||||||
UserName = username;
|
UserName = username;
|
||||||
|
@ -1,39 +1,106 @@
|
|||||||
using MySql.Data.MySqlClient;
|
using MySql.Data.MySqlClient;
|
||||||
using Milimoe.FunGame.Core.Api.Utility;
|
using Milimoe.FunGame.Core.Api.Utility;
|
||||||
|
using Milimoe.FunGame.Core.Library.Server;
|
||||||
|
|
||||||
namespace Milimoe.FunGame.Server.Utility.DataUtility
|
namespace Milimoe.FunGame.Server.Utility.DataUtility
|
||||||
{
|
{
|
||||||
public class MySQLConnection
|
public class ConnectProperties
|
||||||
{
|
{
|
||||||
public static MySqlConnection? Connection = null;
|
|
||||||
|
|
||||||
public static string Name { get; set; } = "";
|
public static string Name { get; set; } = "";
|
||||||
public static string DataSource { get; set; } = "";
|
public static string DataSource { get; set; } = "";
|
||||||
public static string Port { get; set; } = "";
|
public static string Port { get; set; } = "";
|
||||||
public static string DataBase { get; set; } = "";
|
public static string DataBase { get; set; } = "";
|
||||||
public static string User { get; set; } = "";
|
public static string User { get; set; } = "";
|
||||||
public static string Password { get; set; } = "";
|
public static string Password { get; set; } = "";
|
||||||
public static string GetConnection { get; set; } = "";
|
|
||||||
|
|
||||||
public static bool Connect(out MySqlConnection? conn)
|
/// <summary>
|
||||||
|
/// 读取MySQL服务器配置文件
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
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;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建SQL连接
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="serverInfo"></param>
|
||||||
|
public MySQLConnection(out SQLServerInfo? serverInfo)
|
||||||
|
{
|
||||||
|
_Connection = Connect(out serverInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关闭连接
|
||||||
|
/// </summary>
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
if (_Connection != null && _Connection.State == System.Data.ConnectionState.Open)
|
||||||
|
{
|
||||||
|
_Connection.Close();
|
||||||
|
}
|
||||||
|
_Connection = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 连接MySQL服务器
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="serverInfo">服务器信息</param>
|
||||||
|
/// <returns>连接对象</returns>
|
||||||
|
/// <exception cref="MySQLConfigException">MySQL服务启动失败:无法找到MySQL配置文件</exception>
|
||||||
|
private MySqlConnection? Connect(out SQLServerInfo? serverInfo)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
GetConnection = GetConnectProperties();
|
string _GetConnection = ConnectProperties.GetConnectProperties();
|
||||||
if (GetConnection != null)
|
if (_GetConnection != null)
|
||||||
{
|
{
|
||||||
string[] DataSetting = GetConnection.Split(";");
|
string[] DataSetting = _GetConnection.Split(";");
|
||||||
if (DataSetting.Length > 1 && DataSetting[0].Length > 14 && DataSetting[1].Length > 8)
|
if (DataSetting.Length > 1 && DataSetting[0].Length > 14 && DataSetting[1].Length > 8)
|
||||||
{
|
{
|
||||||
ServerHelper.WriteLine("Connect -> MySQL://" + DataSetting[0][14..] + ":" + DataSetting[1][8..]);
|
ServerHelper.WriteLine("Connect -> MySQL://" + DataSetting[0][14..] + ":" + DataSetting[1][8..]);
|
||||||
}
|
}
|
||||||
Connection = new MySqlConnection(GetConnection);
|
_Connection = new MySqlConnection(_GetConnection);
|
||||||
Connection.Open();
|
_Connection.Open();
|
||||||
if (Connection.State == System.Data.ConnectionState.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服务器连接成功");
|
ServerHelper.WriteLine("Connected: MySQL服务器连接成功");
|
||||||
conn = Connection;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -45,32 +112,9 @@ namespace Milimoe.FunGame.Server.Utility.DataUtility
|
|||||||
{
|
{
|
||||||
ServerHelper.Error(e);
|
ServerHelper.Error(e);
|
||||||
}
|
}
|
||||||
conn = Connection;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Close()
|
serverInfo = _ServerInfo;
|
||||||
{
|
return _Connection;
|
||||||
if (Connection != null && Connection.State == System.Data.ConnectionState.Open)
|
|
||||||
{
|
|
||||||
Connection.Close();
|
|
||||||
}
|
|
||||||
Connection = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string GetConnectProperties()
|
|
||||||
{
|
|
||||||
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 Exception("找不到配置文件。"));
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,165 +1,90 @@
|
|||||||
using System.Data;
|
using System.Data;
|
||||||
using MySql.Data.MySqlClient;
|
using MySql.Data.MySqlClient;
|
||||||
using Milimoe.FunGame.Core.Api.Data;
|
using Milimoe.FunGame.Core.Api.Data;
|
||||||
using Milimoe.FunGame.Core.Library.Common.Network;
|
|
||||||
using Milimoe.FunGame.Core.Library.Constant;
|
using Milimoe.FunGame.Core.Library.Constant;
|
||||||
|
using Milimoe.FunGame.Core.Library.Server;
|
||||||
using Milimoe.FunGame.Server.Utility.DataUtility;
|
using Milimoe.FunGame.Server.Utility.DataUtility;
|
||||||
|
|
||||||
namespace Milimoe.FunGame.Server.Utility
|
namespace Milimoe.FunGame.Server.Utility
|
||||||
{
|
{
|
||||||
internal class MySQLHelper : SQLHelper
|
public class MySQLHelper : SQLHelper
|
||||||
{
|
{
|
||||||
public new string Script { get; set; } = "";
|
public override string Script { get; set; } = "";
|
||||||
public new EntityType EntityType => _EntityType;
|
public override CommandType CommandType { get; set; } = CommandType.Text;
|
||||||
public new object Entity => _Entity;
|
public MySqlParameter[] Parameters { get; set; }
|
||||||
public new SQLResult Result => _Result;
|
public override SQLResult Result => _Result;
|
||||||
public override SQLServerInfo ServerInfo
|
public override SQLServerInfo ServerInfo => _ServerInfo ?? SQLServerInfo.Create();
|
||||||
{
|
public MySQLConnection? Connection => _Connection;
|
||||||
get
|
public override int UpdateRows => _UpdateRows;
|
||||||
{
|
public override DataSet DataSet => _DataSet;
|
||||||
return _ServerInfo ?? SQLServerInfo.Create();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public new int UpdateRows => _UpdateRows;
|
|
||||||
public DataSet DataSet => _DataSet;
|
|
||||||
|
|
||||||
private EntityType _EntityType;
|
private SQLResult _Result = SQLResult.Success;
|
||||||
private object _Entity = General.EntityInstance;
|
|
||||||
private SQLResult _Result;
|
|
||||||
private SQLServerInfo? _ServerInfo;
|
private SQLServerInfo? _ServerInfo;
|
||||||
private int _UpdateRows = 0;
|
private int _UpdateRows = 0;
|
||||||
private DataSet _DataSet = new DataSet();
|
private DataSet _DataSet = new();
|
||||||
|
private MySQLConnection? _Connection;
|
||||||
|
|
||||||
public static SQLHelper GetHelper()
|
/// <summary>
|
||||||
|
/// 创建MySQLHelper实例
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="script"></param>
|
||||||
|
/// <param name="type"></param>
|
||||||
|
/// <param name="parameters"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static MySQLHelper GetHelper(string script = "", CommandType type = CommandType.Text, params MySqlParameter[] parameters)
|
||||||
{
|
{
|
||||||
return new MySQLHelper();
|
return new MySQLHelper(script, type, parameters);
|
||||||
}
|
|
||||||
|
|
||||||
public override SQLResult Execute()
|
|
||||||
{
|
|
||||||
return SQLResult.NotFound;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DataSet ExecuteDataSet()
|
|
||||||
{
|
|
||||||
return _DataSet;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 执行一个sql命令
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="type">存储过程, 文本, 等等</param>
|
|
||||||
/// <param name="sql">存储过程名称或者sql语句</param>
|
|
||||||
/// <param name="parameters">执行命令的参数</param>
|
|
||||||
/// <returns>执行命令所影响的行数</returns>
|
|
||||||
private int Execute(CommandType type, string sql, params MySqlParameter[] parameters)
|
|
||||||
{
|
|
||||||
MySqlCommand cmd = new();
|
|
||||||
|
|
||||||
PrepareCommand(cmd, null, type, sql, parameters);
|
|
||||||
|
|
||||||
int updaterow = cmd.ExecuteNonQuery();
|
|
||||||
return updaterow;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 返回DataSet
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="type">存储过程, 文本, 等等</param>
|
|
||||||
/// <param name="sql">存储过程名称或者sql语句</param>
|
|
||||||
/// <param name="parameters">执行命令所用参数的集合</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private DataSet ExecuteDataSet(CommandType type, string sql, params MySqlParameter[] parameters)
|
|
||||||
{
|
|
||||||
MySqlCommand cmd = new();
|
|
||||||
DataSet ds = new();
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
PrepareCommand(cmd, null, type, sql, parameters);
|
|
||||||
|
|
||||||
MySqlDataAdapter adapter = new()
|
|
||||||
{
|
|
||||||
SelectCommand = cmd
|
|
||||||
};
|
|
||||||
adapter.Fill(ds);
|
|
||||||
|
|
||||||
//清 除参数
|
|
||||||
cmd.Parameters.Clear();
|
|
||||||
return ds;
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
ServerHelper.Error(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ds;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 返回插入值ID
|
/// 执行一个命令
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="type">存储过程, 文本, 等等</param>
|
/// <param name="Result">执行结果</param>
|
||||||
/// <param name="sql">存储过程名称或者sql语句</param>
|
/// <returns>影响的行数</returns>
|
||||||
/// <param name="parameters">执行命令所用参数的集合</param>
|
public override int Execute(out SQLResult Result)
|
||||||
/// <returns></returns>
|
|
||||||
private object ExecuteNonExist(CommandType type, string sql, params MySqlParameter[] parameters)
|
|
||||||
{
|
{
|
||||||
MySqlCommand cmd = new();
|
_Connection = new MySQLConnection(out _ServerInfo);
|
||||||
|
ServerHelper.WriteLine("SQLQuery -> " + Script);
|
||||||
PrepareCommand(cmd, null, type, sql, parameters);
|
_UpdateRows = MySQLManager.Execute(this, out Result);
|
||||||
cmd.ExecuteNonQuery();
|
Close();
|
||||||
|
return _UpdateRows;
|
||||||
return cmd.LastInsertedId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 准备执行一个命令
|
/// 查询DataSet
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="cmd">命令</param>
|
/// <param name="Result">执行结果</param>
|
||||||
/// <param name="trans">事务</param>
|
/// <returns>结果集</returns>
|
||||||
/// <param name="type">存储过程或者文本</param>
|
public override DataSet ExecuteDataSet(out SQLResult Result)
|
||||||
/// <param name="sql">sql语句</param>
|
|
||||||
/// <param name="parameters">执行命令的参数</param>
|
|
||||||
private void PrepareCommand(MySqlCommand cmd, MySqlTransaction? trans, CommandType type, string sql, MySqlParameter[] parameters)
|
|
||||||
{
|
{
|
||||||
MySqlConnection? conn = MySQLConnection.Connection;
|
_Connection = new MySQLConnection(out _ServerInfo);
|
||||||
if (conn != null && conn.State != ConnectionState.Open)
|
ServerHelper.WriteLine("SQLQuery -> " + Script);
|
||||||
conn.Open();
|
_DataSet = MySQLManager.ExecuteDataSet(this, out Result);
|
||||||
|
_UpdateRows = _DataSet.Tables[0].Rows.Count;
|
||||||
|
Close();
|
||||||
|
return DataSet;
|
||||||
|
}
|
||||||
|
|
||||||
cmd.Connection = conn;
|
/// <summary>
|
||||||
cmd.CommandText = sql;
|
/// 关闭连接
|
||||||
|
/// </summary>
|
||||||
if (trans != null)
|
public override void Close()
|
||||||
{
|
{
|
||||||
cmd.Transaction = trans;
|
_Connection?.Close();
|
||||||
}
|
ServerHelper.WriteLine("Connection Release");
|
||||||
|
|
||||||
cmd.CommandType = type;
|
|
||||||
|
|
||||||
if (parameters != null)
|
|
||||||
{
|
|
||||||
foreach (MySqlParameter parm in parameters)
|
|
||||||
{
|
|
||||||
cmd.Parameters.Add(parm);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建SQLHelper
|
/// 创建SQLHelper
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="type">获取实体类的类型</param>
|
/// <param name="script">存储过程名称或者script语句</param>
|
||||||
/// <param name="info">SQL服务器信息</param>
|
/// <param name="type">存储过程, 文本, 等等</param>
|
||||||
/// <param name="result">执行结果</param>
|
/// <param name="parameters">执行命令所用参数的集合</param>
|
||||||
/// <param name="rows">更新的行数</param>
|
private MySQLHelper(string script, CommandType type, params MySqlParameter[] parameters)
|
||||||
private MySQLHelper(EntityType type = EntityType.Empty, SQLServerInfo? info = null, SQLResult result = SQLResult.Success, int rows = 0)
|
|
||||||
{
|
{
|
||||||
_EntityType = type;
|
Script = script;
|
||||||
if (info == null) _ServerInfo = SQLServerInfo.Create();
|
CommandType = type;
|
||||||
else _ServerInfo = info;
|
Parameters = parameters;
|
||||||
_Result = result;
|
|
||||||
_UpdateRows = rows;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,83 +1,140 @@
|
|||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Text;
|
using MySql.Data.MySqlClient;
|
||||||
using Milimoe.FunGame.Core.Api.Data;
|
|
||||||
using Milimoe.FunGame.Core.Library.Constant;
|
using Milimoe.FunGame.Core.Library.Constant;
|
||||||
using Milimoe.FunGame.Core.Service;
|
using Milimoe.FunGame.Core.Service;
|
||||||
|
|
||||||
namespace Milimoe.FunGame.Server.Utility
|
namespace Milimoe.FunGame.Server.Utility
|
||||||
{
|
{
|
||||||
public class MySQLManager : SQLManager
|
public class MySQLManager
|
||||||
{
|
{
|
||||||
public new SQLHelper SQLHelper { get; }
|
/// <summary>
|
||||||
|
/// 执行Script命令
|
||||||
public MySQLManager(SQLHelper SQLHelper)
|
/// </summary>
|
||||||
|
/// <param name="Helper">MySQLHelper</param>
|
||||||
|
/// <param name="Result">执行结果</param>
|
||||||
|
/// <returns>影响的行数</returns>
|
||||||
|
public static int Execute(MySQLHelper Helper, out SQLResult Result)
|
||||||
{
|
{
|
||||||
this.SQLHelper = SQLHelper;
|
MySqlCommand cmd = new();
|
||||||
|
int updaterow;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
PrepareCommand(Helper, cmd);
|
||||||
|
updaterow = cmd.ExecuteNonQuery();
|
||||||
|
if (updaterow > 0)
|
||||||
|
{
|
||||||
|
Result = SQLResult.Success;
|
||||||
|
}
|
||||||
|
else Result = SQLResult.NotFound;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
ServerHelper.Error(e);
|
||||||
|
updaterow = -1;
|
||||||
|
Result = SQLResult.Fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
return updaterow;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int Add(StringBuilder sql, ref SQLResult result)
|
/// <summary>
|
||||||
|
/// 返回DataSet
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Helper">MySQLHelper</param>
|
||||||
|
/// <param name="Result">执行结果</param>
|
||||||
|
/// <returns>结果集</returns>
|
||||||
|
public static DataSet ExecuteDataSet(MySQLHelper Helper, out SQLResult Result)
|
||||||
{
|
{
|
||||||
return 0;
|
MySqlCommand cmd = new();
|
||||||
|
DataSet ds = new();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
PrepareCommand(Helper, cmd);
|
||||||
|
|
||||||
|
MySqlDataAdapter adapter = new()
|
||||||
|
{
|
||||||
|
SelectCommand = cmd
|
||||||
|
};
|
||||||
|
adapter.Fill(ds);
|
||||||
|
|
||||||
|
//清除参数
|
||||||
|
cmd.Parameters.Clear();
|
||||||
|
Result = SQLResult.Success;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
ServerHelper.Error(e);
|
||||||
|
Result = SQLResult.Fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ds;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int Add(string sql, ref SQLResult result)
|
/// <summary>
|
||||||
|
/// 返回插入值ID
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Helper">MySQLHelper</param>
|
||||||
|
/// <param name="Result">执行结果</param>
|
||||||
|
/// <returns>插入值ID</returns>
|
||||||
|
public static object ExecuteAndGetLastInsertedID(MySQLHelper Helper, out SQLResult Result)
|
||||||
{
|
{
|
||||||
return 0;
|
MySqlCommand cmd = new();
|
||||||
|
int updaterow;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
PrepareCommand(Helper, cmd);
|
||||||
|
updaterow = cmd.ExecuteNonQuery();
|
||||||
|
if (updaterow > 0)
|
||||||
|
{
|
||||||
|
Result = SQLResult.Success;
|
||||||
|
}
|
||||||
|
else Result = SQLResult.NotFound;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
ServerHelper.Error(e);
|
||||||
|
Result = SQLResult.Fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmd.LastInsertedId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override SQLResult Execute()
|
/// <summary>
|
||||||
|
/// 准备执行一个命令
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Helper">MySQLHelper</param>
|
||||||
|
/// <param name="cmd">命令对象</param>
|
||||||
|
/// <param name="trans">事务</param>
|
||||||
|
public static void PrepareCommand(MySQLHelper Helper, MySqlCommand cmd, MySqlTransaction? trans = null)
|
||||||
{
|
{
|
||||||
return SQLResult.NotFound;
|
if (Helper.Connection != null)
|
||||||
}
|
{
|
||||||
|
MySqlConnection? conn = Helper.Connection.Connection;
|
||||||
|
|
||||||
public override SQLResult Execute(StringBuilder sql)
|
if (conn != null && conn.State != ConnectionState.Open)
|
||||||
{
|
conn.Open();
|
||||||
return SQLResult.NotFound;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override SQLResult Execute(string sql)
|
cmd.Connection = conn;
|
||||||
{
|
cmd.CommandText = Helper.Script;
|
||||||
return SQLResult.NotFound;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override DataSet ExecuteDataSet(StringBuilder sql)
|
if (trans != null)
|
||||||
{
|
{
|
||||||
return new DataSet();
|
cmd.Transaction = trans;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override DataSet ExecuteDataSet(string sql)
|
cmd.CommandType = Helper.CommandType;
|
||||||
{
|
|
||||||
return new DataSet();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override object Query(EntityType type, StringBuilder sql)
|
if (Helper.Parameters != null)
|
||||||
{
|
{
|
||||||
return General.EntityInstance;
|
foreach (MySqlParameter parm in Helper.Parameters)
|
||||||
}
|
{
|
||||||
|
cmd.Parameters.Add(parm);
|
||||||
public override object Query(EntityType type, string sql)
|
}
|
||||||
{
|
}
|
||||||
return General.EntityInstance;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public override int Remove(StringBuilder sql, ref SQLResult result)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int Remove(string sql, ref SQLResult result)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int Update(StringBuilder sql, ref SQLResult result)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int Update(string sql, ref SQLResult result)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user