Guest User

Untitled

a guest
Aug 25th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. class DatabaseConnection
  2. {
  3. //properties
  4. private MySqlConnection _connection;
  5. public MySqlConnection Connection { get { return _connection; } }
  6. public AccountLogin AccountCredentials { get; }
  7. public DatabaseLogin DatabaseCredentials { get; }
  8.  
  9. //constructor
  10. public DatabaseConnection(AccountLogin _AccCred,
  11. DatabaseLogin _DBCred)
  12. {
  13. AccountCredentials = _AccCred;
  14. DatabaseCredentials = _DBCred;
  15. }
  16.  
  17. public void Connect()
  18. {
  19. if (Connection != null)
  20. {
  21. return;
  22. }
  23.  
  24. string[] UserInputs =
  25. {
  26. DatabaseCredentials?.DatabaseName,
  27. DatabaseCredentials?.Server,
  28. DatabaseCredentials?.Port,
  29. AccountCredentials?.Password,
  30. AccountCredentials?.Username
  31. };
  32. bool ChkInpts = Validators.NullStringValidator(UserInputs);
  33.  
  34. if (!ChkInpts)
  35. {
  36. string ConnInfo = "server=" + DatabaseCredentials.Server + ";" +
  37. "user=" + AccountCredentials.Username + ";" +
  38. "database=" + DatabaseCredentials.DatabaseName +
  39. "port=" + DatabaseCredentials.Port +
  40. "password=" + AccountCredentials.Password;
  41. _connection = new MySqlConnection(ConnInfo);
  42.  
  43. try
  44. {
  45. Console.WriteLine("Connecting to " + DatabaseCredentials.DatabaseName + "...");
  46. _connection.Open();
  47. Console.WriteLine("Connection to " + DatabaseCredentials.DatabaseName + " successful.");
  48. }
  49.  
  50. catch (Exception ex)
  51. {
  52. Console.WriteLine(ex.ToString());
  53. }
  54. }
  55. }
  56. public void Close()
  57. {
  58. _connection.Close();
  59. }
  60. }
  61.  
  62. class AccountLogin
  63. {
  64. public string Username { get; }
  65. public string Password { get; }
  66.  
  67. public AccountLogin(string _username, string _password)
  68. {
  69. Username = _username;
  70. Password = _password;
  71. }
  72. }
  73.  
  74. class DatabaseLogin
  75. {
  76. public string DatabaseName { get; }
  77. public string Server { get; }
  78. public string Port { get; }
  79.  
  80. public DatabaseLogin(string _server, string _DatabaseName, string _port)
  81. {
  82. DatabaseName = _DatabaseName;
  83. Server = _server;
  84. Port = _port;
  85. }
  86. }
  87.  
  88. static class Validators
  89. {
  90. public static bool NullStringValidator(string[] _inputs)
  91. {
  92. bool _result = false;
  93. for (int i = 0; i < _inputs.Length; i++)
  94. {
  95. if (string.IsNullOrEmpty(_inputs[i]))
  96. {
  97. return _result = true;
  98. }
  99. }
  100. return _result;
  101. }
  102. }
  103.  
  104. AccountLogin accountLogin
  105.  
  106. string ConnInfo = "server=" + DatabaseCredentials.Server + ";" +
  107. "user=" + AccountCredentials.Username + ";" +
  108. "database=" + DatabaseCredentials.DatabaseName +
  109. "port=" + DatabaseCredentials.Port +
  110. "password=" + AccountCredentials.Password;
  111.  
  112. StringBuilder
  113.  
  114. String.Format("server={0};user={1};...", DatabaseCredentials.Server, AccountCredentials.Username,...);
  115.  
  116. $"server={DatabaseCredentials.Server};user={AccountCredentials.Username};..."
  117.  
  118. using (DatabaseConnection dc = new DatabaseConnection(...))
  119. {
  120. // Use the connection...
  121. }
  122.  
  123. public void Close()
  124. {
  125. _connection.Close();
  126. }
Add Comment
Please, Sign In to add comment