mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-22 03:59:36 +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.Others;
|
||||
using Milimoe.FunGame.Server.Utility;
|
||||
using Milimoe.FunGame.Server.Utility.DataUtility;
|
||||
|
||||
Console.Title = Config.SERVER_NAME;
|
||||
Console.WriteLine(FunGameInfo.GetInfo((FunGameInfo.FunGame)Config.FunGameType));
|
||||
@ -64,7 +63,6 @@ void StartServer()
|
||||
ServerHelper.WriteLine("未检测到配置文件,将自动创建配置文件 . . .");
|
||||
INIHelper.Init((FunGameInfo.FunGame)Config.FunGameType);
|
||||
ServerHelper.WriteLine("配置文件FunGame.ini创建成功,请修改该配置文件,然后重启服务器。");
|
||||
ServerHelper.WriteLine("请输入 help 来获取帮助,输入 quit 关闭服务器。");
|
||||
return;
|
||||
}
|
||||
else
|
||||
@ -72,21 +70,19 @@ void StartServer()
|
||||
ServerHelper.GetServerSettings();
|
||||
Console.Title = Config.SERVER_NAME + " - FunGame Server Port: " + Config.SERVER_PORT;
|
||||
}
|
||||
ServerHelper.WriteLine("请输入 help 来获取帮助,输入 quit 关闭服务器。");
|
||||
|
||||
MySQLConnection.Close();
|
||||
|
||||
// 连接MySQL服务器
|
||||
if (!MySQLConnection.Connect(out _))
|
||||
// 测试MySQL服务器连接
|
||||
if (TestSQLConnection() != SQLResult.Success)
|
||||
{
|
||||
Running = false;
|
||||
throw new ServerErrorException();
|
||||
throw new SQLQueryException();
|
||||
}
|
||||
|
||||
// 创建监听
|
||||
ListeningSocket = ServerSocket.StartListening();
|
||||
|
||||
// 开始监听连接
|
||||
//ServerSocket.Listen(Config.MAX_PLAYERS);
|
||||
ServerHelper.WriteLine("Listen -> " + Config.SERVER_PORT);
|
||||
ServerHelper.WriteLine("服务器启动成功,开始监听 . . .");
|
||||
|
||||
@ -127,7 +123,7 @@ void StartServer()
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (e.Message.Equals("服务器遇到问题需要关闭,请重新启动服务器!"))
|
||||
if (e.Message.Equals(new ServerErrorException().Message))
|
||||
{
|
||||
if (ListeningSocket != null)
|
||||
{
|
||||
@ -183,3 +179,12 @@ bool Send(ClientSocket socket)
|
||||
ServerHelper.WriteLine("无法传输数据,与客户端的连接可能丢失。");
|
||||
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.Server.Others;
|
||||
using Milimoe.FunGame.Server.Utility;
|
||||
using System.Data;
|
||||
|
||||
namespace Milimoe.FunGame.Server.Model
|
||||
{
|
||||
@ -26,6 +27,7 @@ namespace Milimoe.FunGame.Server.Model
|
||||
private string UserName = "";
|
||||
private string Password = "";
|
||||
private int FailedTimes = 0; // 超过一定次数断开连接
|
||||
private MySQLHelper SQLHelper = MySQLHelper.GetHelper();
|
||||
|
||||
public ServerModel(ClientSocket socket, bool running)
|
||||
{
|
||||
@ -70,8 +72,11 @@ namespace Milimoe.FunGame.Server.Model
|
||||
if (username != null && password != null)
|
||||
{
|
||||
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() != "")
|
||||
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(type) + "] AutoKey: 已确认");
|
||||
UserName = username;
|
||||
|
@ -1,39 +1,106 @@
|
||||
using MySql.Data.MySqlClient;
|
||||
using Milimoe.FunGame.Core.Api.Utility;
|
||||
using Milimoe.FunGame.Core.Library.Server;
|
||||
|
||||
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 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; } = "";
|
||||
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
|
||||
{
|
||||
GetConnection = GetConnectProperties();
|
||||
if (GetConnection != null)
|
||||
string _GetConnection = ConnectProperties.GetConnectProperties();
|
||||
if (_GetConnection != null)
|
||||
{
|
||||
string[] DataSetting = GetConnection.Split(";");
|
||||
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)
|
||||
_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服务器连接成功");
|
||||
conn = Connection;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -45,32 +112,9 @@ namespace Milimoe.FunGame.Server.Utility.DataUtility
|
||||
{
|
||||
ServerHelper.Error(e);
|
||||
}
|
||||
conn = Connection;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void Close()
|
||||
{
|
||||
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 "";
|
||||
serverInfo = _ServerInfo;
|
||||
return _Connection;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,165 +1,90 @@
|
||||
using System.Data;
|
||||
using MySql.Data.MySqlClient;
|
||||
using Milimoe.FunGame.Core.Api.Data;
|
||||
using Milimoe.FunGame.Core.Library.Common.Network;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
using Milimoe.FunGame.Core.Library.Server;
|
||||
using Milimoe.FunGame.Server.Utility.DataUtility;
|
||||
|
||||
namespace Milimoe.FunGame.Server.Utility
|
||||
{
|
||||
internal class MySQLHelper : SQLHelper
|
||||
public class MySQLHelper : SQLHelper
|
||||
{
|
||||
public new string Script { get; set; } = "";
|
||||
public new EntityType EntityType => _EntityType;
|
||||
public new object Entity => _Entity;
|
||||
public new SQLResult Result => _Result;
|
||||
public override SQLServerInfo ServerInfo
|
||||
{
|
||||
get
|
||||
{
|
||||
return _ServerInfo ?? SQLServerInfo.Create();
|
||||
}
|
||||
}
|
||||
public new int UpdateRows => _UpdateRows;
|
||||
public DataSet DataSet => _DataSet;
|
||||
public override string Script { get; set; } = "";
|
||||
public override CommandType CommandType { get; set; } = CommandType.Text;
|
||||
public MySqlParameter[] Parameters { get; set; }
|
||||
public override SQLResult Result => _Result;
|
||||
public override SQLServerInfo ServerInfo => _ServerInfo ?? SQLServerInfo.Create();
|
||||
public MySQLConnection? Connection => _Connection;
|
||||
public override int UpdateRows => _UpdateRows;
|
||||
public override DataSet DataSet => _DataSet;
|
||||
|
||||
private EntityType _EntityType;
|
||||
private object _Entity = General.EntityInstance;
|
||||
private SQLResult _Result;
|
||||
private SQLResult _Result = SQLResult.Success;
|
||||
private SQLServerInfo? _ServerInfo;
|
||||
private int _UpdateRows = 0;
|
||||
private DataSet _DataSet = new DataSet();
|
||||
|
||||
public static SQLHelper GetHelper()
|
||||
{
|
||||
return new MySQLHelper();
|
||||
}
|
||||
|
||||
public override SQLResult Execute()
|
||||
{
|
||||
return SQLResult.NotFound;
|
||||
}
|
||||
|
||||
public DataSet ExecuteDataSet()
|
||||
{
|
||||
return _DataSet;
|
||||
}
|
||||
private DataSet _DataSet = new();
|
||||
private MySQLConnection? _Connection;
|
||||
|
||||
/// <summary>
|
||||
/// 执行一个sql命令
|
||||
/// 创建MySQLHelper实例
|
||||
/// </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>
|
||||
/// <param name="script"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="parameters"></param>
|
||||
/// <returns></returns>
|
||||
private DataSet ExecuteDataSet(CommandType type, string sql, params MySqlParameter[] parameters)
|
||||
public static MySQLHelper GetHelper(string script = "", CommandType type = CommandType.Text, 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;
|
||||
return new MySQLHelper(script, type, parameters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回插入值ID
|
||||
/// 执行一个命令
|
||||
/// </summary>
|
||||
/// <param name="type">存储过程, 文本, 等等</param>
|
||||
/// <param name="sql">存储过程名称或者sql语句</param>
|
||||
/// <param name="parameters">执行命令所用参数的集合</param>
|
||||
/// <returns></returns>
|
||||
private object ExecuteNonExist(CommandType type, string sql, params MySqlParameter[] parameters)
|
||||
/// <param name="Result">执行结果</param>
|
||||
/// <returns>影响的行数</returns>
|
||||
public override int Execute(out SQLResult Result)
|
||||
{
|
||||
MySqlCommand cmd = new();
|
||||
|
||||
PrepareCommand(cmd, null, type, sql, parameters);
|
||||
cmd.ExecuteNonQuery();
|
||||
|
||||
return cmd.LastInsertedId;
|
||||
_Connection = new MySQLConnection(out _ServerInfo);
|
||||
ServerHelper.WriteLine("SQLQuery -> " + Script);
|
||||
_UpdateRows = MySQLManager.Execute(this, out Result);
|
||||
Close();
|
||||
return _UpdateRows;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 准备执行一个命令
|
||||
/// 查询DataSet
|
||||
/// </summary>
|
||||
/// <param name="cmd">命令</param>
|
||||
/// <param name="trans">事务</param>
|
||||
/// <param name="type">存储过程或者文本</param>
|
||||
/// <param name="sql">sql语句</param>
|
||||
/// <param name="parameters">执行命令的参数</param>
|
||||
private void PrepareCommand(MySqlCommand cmd, MySqlTransaction? trans, CommandType type, string sql, MySqlParameter[] parameters)
|
||||
/// <param name="Result">执行结果</param>
|
||||
/// <returns>结果集</returns>
|
||||
public override DataSet ExecuteDataSet(out SQLResult Result)
|
||||
{
|
||||
MySqlConnection? conn = MySQLConnection.Connection;
|
||||
if (conn != null && conn.State != ConnectionState.Open)
|
||||
conn.Open();
|
||||
|
||||
cmd.Connection = conn;
|
||||
cmd.CommandText = sql;
|
||||
|
||||
if (trans != null)
|
||||
{
|
||||
cmd.Transaction = trans;
|
||||
_Connection = new MySQLConnection(out _ServerInfo);
|
||||
ServerHelper.WriteLine("SQLQuery -> " + Script);
|
||||
_DataSet = MySQLManager.ExecuteDataSet(this, out Result);
|
||||
_UpdateRows = _DataSet.Tables[0].Rows.Count;
|
||||
Close();
|
||||
return DataSet;
|
||||
}
|
||||
|
||||
cmd.CommandType = type;
|
||||
|
||||
if (parameters != null)
|
||||
/// <summary>
|
||||
/// 关闭连接
|
||||
/// </summary>
|
||||
public override void Close()
|
||||
{
|
||||
foreach (MySqlParameter parm in parameters)
|
||||
{
|
||||
cmd.Parameters.Add(parm);
|
||||
}
|
||||
}
|
||||
_Connection?.Close();
|
||||
ServerHelper.WriteLine("Connection Release");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建SQLHelper
|
||||
/// </summary>
|
||||
/// <param name="type">获取实体类的类型</param>
|
||||
/// <param name="info">SQL服务器信息</param>
|
||||
/// <param name="result">执行结果</param>
|
||||
/// <param name="rows">更新的行数</param>
|
||||
private MySQLHelper(EntityType type = EntityType.Empty, SQLServerInfo? info = null, SQLResult result = SQLResult.Success, int rows = 0)
|
||||
/// <param name="script">存储过程名称或者script语句</param>
|
||||
/// <param name="type">存储过程, 文本, 等等</param>
|
||||
/// <param name="parameters">执行命令所用参数的集合</param>
|
||||
private MySQLHelper(string script, CommandType type, params MySqlParameter[] parameters)
|
||||
{
|
||||
_EntityType = type;
|
||||
if (info == null) _ServerInfo = SQLServerInfo.Create();
|
||||
else _ServerInfo = info;
|
||||
_Result = result;
|
||||
_UpdateRows = rows;
|
||||
Script = script;
|
||||
CommandType = type;
|
||||
Parameters = parameters;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,83 +1,140 @@
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
using Milimoe.FunGame.Core.Api.Data;
|
||||
using MySql.Data.MySqlClient;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
using Milimoe.FunGame.Core.Service;
|
||||
|
||||
namespace Milimoe.FunGame.Server.Utility
|
||||
{
|
||||
public class MySQLManager : SQLManager
|
||||
public class MySQLManager
|
||||
{
|
||||
public new SQLHelper SQLHelper { get; }
|
||||
/// <summary>
|
||||
/// 执行Script命令
|
||||
/// </summary>
|
||||
/// <param name="Helper">MySQLHelper</param>
|
||||
/// <param name="Result">执行结果</param>
|
||||
/// <returns>影响的行数</returns>
|
||||
public static int Execute(MySQLHelper Helper, out SQLResult Result)
|
||||
{
|
||||
MySqlCommand cmd = new();
|
||||
int updaterow;
|
||||
|
||||
public MySQLManager(SQLHelper SQLHelper)
|
||||
try
|
||||
{
|
||||
this.SQLHelper = SQLHelper;
|
||||
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;
|
||||
}
|
||||
|
||||
public override int Add(StringBuilder sql, ref SQLResult result)
|
||||
{
|
||||
return 0;
|
||||
return updaterow;
|
||||
}
|
||||
|
||||
public override int Add(string 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;
|
||||
}
|
||||
|
||||
public override SQLResult Execute()
|
||||
{
|
||||
return SQLResult.NotFound;
|
||||
return ds;
|
||||
}
|
||||
|
||||
public override SQLResult Execute(StringBuilder sql)
|
||||
/// <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 SQLResult.NotFound;
|
||||
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;
|
||||
}
|
||||
|
||||
public override SQLResult Execute(string sql)
|
||||
{
|
||||
return SQLResult.NotFound;
|
||||
return cmd.LastInsertedId;
|
||||
}
|
||||
|
||||
public override DataSet ExecuteDataSet(StringBuilder sql)
|
||||
/// <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 new DataSet();
|
||||
if (Helper.Connection != null)
|
||||
{
|
||||
MySqlConnection? conn = Helper.Connection.Connection;
|
||||
|
||||
if (conn != null && conn.State != ConnectionState.Open)
|
||||
conn.Open();
|
||||
|
||||
cmd.Connection = conn;
|
||||
cmd.CommandText = Helper.Script;
|
||||
|
||||
if (trans != null)
|
||||
{
|
||||
cmd.Transaction = trans;
|
||||
}
|
||||
|
||||
public override DataSet ExecuteDataSet(string sql)
|
||||
{
|
||||
return new DataSet();
|
||||
}
|
||||
cmd.CommandType = Helper.CommandType;
|
||||
|
||||
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