MySQLConnector.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. string connstring = $"Server={Server}; database={DatabaseName}; UID={UserName}; password={Password}";
  26. Connection = new MySqlConnection(connstring);
  27. Connection.Open();
  28. return true;
  29. }
  30. if (Connection.State != System.Data.ConnectionState.Open)
  31. {
  32. Connection.Open();
  33. return true;
  34. }
  35. return false;
  36. }
  37. public long SQLInsert(string sql)
  38. {
  39. long lastID = 0;
  40. bool connected = IsConnect();
  41. if (connected)
  42. {
  43. SQLcom = new(sql, Connection);
  44. SQLcom.ExecuteNonQuery();
  45. SQLcom.Dispose();
  46. lastID = SQLcom.LastInsertedId;
  47. }
  48. else
  49. Console.WriteLine("Not connected to DB.");
  50. return lastID;
  51. }
  52. public uint SQLGetID(string sql)
  53. {
  54. bool connected = IsConnect();
  55. if (connected)
  56. {
  57. SQLcom = new(sql, Connection);
  58. object obj = SQLcom.ExecuteScalar();
  59. return (uint)obj;
  60. }
  61. else return 0;
  62. }
  63. public void Close()
  64. {
  65. Connection.Close();
  66. Console.Write("Db connection closed");
  67. }
  68. }
  69. }