mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-12-05 16:16:34 +00:00
parent
690c85f2e2
commit
72b3e5b85e
@ -30,6 +30,7 @@ namespace Milimoe.FunGame.Server.Utility
|
||||
private int _UpdateRows = 0;
|
||||
private DataSet _DataSet = new();
|
||||
private MySQLConnection? _Connection;
|
||||
private MySqlTransaction? _Transaction;
|
||||
private readonly ServerModel? ServerModel;
|
||||
private readonly bool _IsOneTime = false;
|
||||
|
||||
@ -141,6 +142,36 @@ namespace Milimoe.FunGame.Server.Utility
|
||||
_Connection = new MySQLConnection(out _ServerInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个SQL事务
|
||||
/// </summary>
|
||||
public void NewTransaction()
|
||||
{
|
||||
_Transaction ??= _Connection?.Connection?.BeginTransaction();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 提交事务
|
||||
/// </summary>
|
||||
public void Commit()
|
||||
{
|
||||
_Transaction?.Commit();
|
||||
_Transaction = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 回滚事务
|
||||
/// </summary>
|
||||
public void Rollback()
|
||||
{
|
||||
_Transaction?.Rollback();
|
||||
_Transaction = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取客户端名称
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private string GetClientName()
|
||||
{
|
||||
if (ServerModel is null) return "";
|
||||
|
||||
@ -20,18 +20,22 @@ namespace Milimoe.FunGame.Server.Utility
|
||||
try
|
||||
{
|
||||
PrepareCommand(Helper, cmd);
|
||||
Helper.NewTransaction();
|
||||
|
||||
updaterow = cmd.ExecuteNonQuery();
|
||||
if (updaterow > 0)
|
||||
{
|
||||
Result = SQLResult.Success;
|
||||
}
|
||||
else Result = SQLResult.NotFound;
|
||||
Helper.Commit();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ServerHelper.Error(e);
|
||||
updaterow = -1;
|
||||
Result = SQLResult.Fail;
|
||||
Helper.Rollback();
|
||||
}
|
||||
|
||||
return updaterow;
|
||||
@ -89,17 +93,21 @@ namespace Milimoe.FunGame.Server.Utility
|
||||
try
|
||||
{
|
||||
PrepareCommand(Helper, cmd);
|
||||
Helper.NewTransaction();
|
||||
|
||||
updaterow = cmd.ExecuteNonQuery();
|
||||
if (updaterow > 0)
|
||||
{
|
||||
Result = SQLResult.Success;
|
||||
}
|
||||
else Result = SQLResult.NotFound;
|
||||
Helper.Commit();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ServerHelper.Error(e);
|
||||
Result = SQLResult.Fail;
|
||||
Helper.Rollback();
|
||||
}
|
||||
|
||||
return cmd.LastInsertedId;
|
||||
@ -110,31 +118,26 @@ namespace Milimoe.FunGame.Server.Utility
|
||||
/// </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)
|
||||
public static void PrepareCommand(MySQLHelper Helper, MySqlCommand cmd)
|
||||
{
|
||||
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)
|
||||
if (conn != null)
|
||||
{
|
||||
cmd.Transaction = trans;
|
||||
}
|
||||
if (conn.State != ConnectionState.Open) conn.Open();
|
||||
|
||||
cmd.CommandType = Helper.CommandType;
|
||||
cmd.Connection = conn;
|
||||
cmd.CommandText = Helper.Script;
|
||||
cmd.CommandType = Helper.CommandType;
|
||||
|
||||
if (Helper.Parameters != null)
|
||||
{
|
||||
foreach (MySqlParameter parm in Helper.Parameters)
|
||||
if (Helper.Parameters != null)
|
||||
{
|
||||
cmd.Parameters.Add(parm);
|
||||
foreach (MySqlParameter parm in Helper.Parameters)
|
||||
{
|
||||
cmd.Parameters.Add(parm);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user