public bool UpdatePlayer(string id, PlayerEntity entity)
{
try
{
MySqlCommand cmd = new MySqlCommand("update player " +
"set killCount=@killCount," +
"deathCount=@deathCount," +
"winCount=@winCount," +
"loseCount=@loseCount " +
"where id=@id", mConn);
cmd.Parameters.AddWithValue("killCount", entity.KillCount);
cmd.Parameters.AddWithValue("deathCount", entity.DeathCount);
cmd.Parameters.AddWithValue("winCount", entity.WinCount);
cmd.Parameters.AddWithValue("loseCount", entity.LoseCount);
cmd.Parameters.AddWithValue("id", id);
cmd.ExecuteNonQuery();
return true;
}
catch (Exception e)
{
Console.WriteLine("PlayerDAO UpdatePlayer Error:" + e.ToString());
return false;
}
}
如图,游戏结算时多个客户端同时请求更新数据库中的数据,报了DataReader的错误,但是我这是更新操作,并没有用到MySqlDataReader,不知道该怎么解决
可能是因为在多个客户端同时请求更新数据库时,由于并发操作,导致了数据读取冲突,从而引发了该错误。
public bool UpdatePlayer(string id, PlayerEntity entity)
{
try
{
MySqlCommand cmd = new MySqlCommand("update player " +
"set killCount=@killCount," +
"deathCount=@deathCount," +
"winCount=@winCount," +
"loseCount=@loseCount " +
"where id=@id", mConn);
cmd.Parameters.AddWithValue("killCount", entity.KillCount);
cmd.Parameters.AddWithValue("deathCount", entity.DeathCount);
cmd.Parameters.AddWithValue("winCount", entity.WinCount);
cmd.Parameters.AddWithValue("loseCount", entity.LoseCount);
cmd.Parameters.AddWithValue("id", id);
mConn.Open();
MySqlTransaction transaction = mConn.BeginTransaction();
try
{
cmd.Transaction = transaction;
cmd.ExecuteNonQuery();
transaction.Commit();
return true;
}
catch (Exception ex)
{
Console.WriteLine("PlayerDAO UpdatePlayer Error:" + ex.ToString());
transaction.Rollback();
return false;
}
}
catch (Exception e)
{
Console.WriteLine("PlayerDAO UpdatePlayer Error:" + e.ToString());
return false;
}
finally
{
mConn.Close();
}
}你试试