MySQLConnector.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using MySql.Data.MySqlClient;
  3. namespace HyperCube
  4. {
  5. public class MySQLConnector
  6. {
  7. private MySQLConnector(){}
  8. private readonly string Server = "dmatter.net";
  9. private readonly string DatabaseName = "documents";
  10. private readonly string UserName = "promsystem";
  11. private readonly string Password = "PrmSystem212";
  12. MySqlCommand SQLcom;
  13. public MySqlConnection Connection { get; set; }
  14. private static MySQLConnector _instance = null;
  15. public static MySQLConnector Instance()
  16. {
  17. if (_instance == null)
  18. _instance = new MySQLConnector();
  19. return _instance;
  20. }
  21. public bool IsConnect()
  22. {
  23. if (Connection == null)
  24. {
  25. if (String.IsNullOrEmpty(DatabaseName))
  26. return false;
  27. string connstring = $"Server={Server}; database={DatabaseName}; UID={UserName}; password={Password}";
  28. Connection = new MySqlConnection(connstring);
  29. Connection.Open();
  30. }
  31. return true;
  32. }
  33. public long SQLInsert(string sql)
  34. {
  35. long lastID = 0;
  36. bool connected = IsConnect();
  37. if (connected)
  38. {
  39. SQLcom = new(sql, Connection);
  40. SQLcom.ExecuteNonQuery();
  41. SQLcom.Dispose();
  42. lastID = SQLcom.LastInsertedId;
  43. }
  44. return lastID;
  45. }
  46. public uint SQLGetID(string sql)
  47. {
  48. bool connected = IsConnect();
  49. if (connected)
  50. {
  51. SQLcom = new(sql, Connection);
  52. object obj = SQLcom.ExecuteScalar();
  53. return (uint)obj;
  54. }
  55. else return 0;
  56. }
  57. public void Close()
  58. {
  59. Connection.Close();
  60. }
  61. }
  62. }