Unity - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(197评价)
价格: 4431.00元
数据库更新数据时报错
淫魔灭世发起了问答2023-05-16
3
回复
183
浏览
        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;
            }
        }

所有回复
  • 淫魔灭世 2023-05-16

    如图,游戏结算时多个客户端同时请求更新数据库中的数据,报了DataReader的错误,但是我这是更新操作,并没有用到MySqlDataReader,不知道该怎么解决

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • 老师_maimai 2023-05-17

    可能是因为在多个客户端同时请求更新数据库时,由于并发操作,导致了数据读取冲突,从而引发了该错误。

    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();
        }
    }你试试

    • 淫魔灭世 2023-05-17

      我查了一下,是因为多个线程同时调用这个方法。在调用这个方法时候加一个线程锁就好了

      (0) 回复
    还有-4条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!