• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共151篇

    Unity - 游戏引擎

关闭

返回栏目

关闭

返回Unity - 游戏引擎栏目

89 - Tool - ConnHelper.cs - 数据库连接助手

作者:

贺及楼

成为作者

更新日期:2023-09-17 10:57:12

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MySql.Data.MySqlClient;
  7. namespace GameServer.Tool
  8. {
  9. class ConnHelper
  10. {
  11. public const string CONNECTIONSTRING = "datasource=127.0.0.1;port=3306;database=tetirsdb;user=root;pwd=root;";
  12. //连接数据库
  13. public static MySqlConnection Connect()
  14. {
  15. MySqlConnection conn = new MySqlConnection(CONNECTIONSTRING);
  16. try
  17. {
  18. conn.Open();
  19. return conn;
  20. }
  21. catch(Exception e)
  22. {
  23. Console.WriteLine("链接数据库的时候实现异常:" + e);
  24. return null;
  25. }
  26. }
  27. //断开数据库
  28. public static void CloseConnection(MySqlConnection conn)
  29. {
  30. if(conn!=null)
  31. conn.Close();
  32. else
  33. {
  34. Console.WriteLine("MySqlConnection不能为空");
  35. }
  36. }
  37. }
  38. }