• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共151篇

    Unity - 游戏引擎

关闭

返回栏目

关闭

返回Unity - 游戏引擎栏目

103 - DAO - ResultDAO.cs - 战绩表操作

作者:

贺及楼

成为作者

更新日期:2023-09-17 10:58:24

  1. class ResultDAO
  2. {
  3. public Result GetResultByUserid( 连接器,用户ID){}
  4. public void UpdateOrAddResult(连接器, 数据结果){}
  5. }
  1. class ResultDAO
  2. {
  3. public Result GetResultByUserid( MySqlConnection conn,int userId){}
  4. public void UpdateOrAddResult(MySqlConnection conn, Result res){}
  5. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using GameServer.Model;
  7. using MySql.Data.MySqlClient;
  8. namespace GameServer.DAO
  9. {
  10. class ResultDAO
  11. {
  12. public Result GetResultByUserid( MySqlConnection conn,int userId)
  13. {
  14. MySqlDataReader reader = null;
  15. try
  16. {
  17. MySqlCommand cmd = new MySqlCommand("select * from result where userid = @userid", conn);
  18. cmd.Parameters.AddWithValue("userid", userId);
  19. reader = cmd.ExecuteReader();
  20. if (reader.Read())
  21. {
  22. int id = reader.GetInt32("id");
  23. int totalCount = reader.GetInt32("totalcount");
  24. int winCount = reader.GetInt32("wincount");
  25. Result res = new Result(id, userId, totalCount, winCount);
  26. return res;
  27. }
  28. else
  29. {
  30. Result res = new Result(-1, userId, 0, 0);
  31. return res;
  32. }
  33. }
  34. catch (Exception e)
  35. {
  36. Console.WriteLine("在GetResultByUserid的时候出现异常:" + e);
  37. }
  38. finally
  39. {
  40. if (reader != null) reader.Close();
  41. }
  42. return null;
  43. }
  44. public void UpdateOrAddResult(MySqlConnection conn, Result res)
  45. {
  46. try
  47. {
  48. MySqlCommand cmd = null;
  49. if (res.Id <= -1)
  50. {
  51. cmd= new MySqlCommand("insert into result set totalcount=@totalcount,wincount=@wincount,userid=@userid", conn);
  52. }
  53. else
  54. {
  55. cmd = new MySqlCommand("update result set totalcount=@totalcount,wincount=@wincount where userid=@userid ", conn);
  56. }
  57. cmd.Parameters.AddWithValue("totalcount", res.TotalCount);
  58. cmd.Parameters.AddWithValue("wincount", res.WinCount);
  59. cmd.Parameters.AddWithValue("userid", res.UserId);
  60. cmd.ExecuteNonQuery();
  61. if (res.Id <= -1)
  62. {
  63. Result tempRes = GetResultByUserid(conn, res.UserId);
  64. res.Id = tempRes.Id;
  65. }
  66. }
  67. catch (Exception e)
  68. {
  69. Console.WriteLine("在UpdateOrAddResult的时候出现异常:" + e);
  70. }
  71. }
  72. }
  73. }