Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DatabaseConnection
- {
- //properties
- private MySqlConnection _connection;
- public MySqlConnection Connection { get { return _connection; } }
- public AccountLogin AccountCredentials { get; }
- public DatabaseLogin DatabaseCredentials { get; }
- //constructor
- public DatabaseConnection(AccountLogin _AccCred,
- DatabaseLogin _DBCred)
- {
- AccountCredentials = _AccCred;
- DatabaseCredentials = _DBCred;
- }
- public void Connect()
- {
- if (Connection != null)
- {
- return;
- }
- string[] UserInputs =
- {
- DatabaseCredentials?.DatabaseName,
- DatabaseCredentials?.Server,
- DatabaseCredentials?.Port,
- AccountCredentials?.Password,
- AccountCredentials?.Username
- };
- bool ChkInpts = Validators.NullStringValidator(UserInputs);
- if (!ChkInpts)
- {
- string ConnInfo = "server=" + DatabaseCredentials.Server + ";" +
- "user=" + AccountCredentials.Username + ";" +
- "database=" + DatabaseCredentials.DatabaseName +
- "port=" + DatabaseCredentials.Port +
- "password=" + AccountCredentials.Password;
- _connection = new MySqlConnection(ConnInfo);
- try
- {
- Console.WriteLine("Connecting to " + DatabaseCredentials.DatabaseName + "...");
- _connection.Open();
- Console.WriteLine("Connection to " + DatabaseCredentials.DatabaseName + " successful.");
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- }
- }
- public void Close()
- {
- _connection.Close();
- }
- }
- class AccountLogin
- {
- public string Username { get; }
- public string Password { get; }
- public AccountLogin(string _username, string _password)
- {
- Username = _username;
- Password = _password;
- }
- }
- class DatabaseLogin
- {
- public string DatabaseName { get; }
- public string Server { get; }
- public string Port { get; }
- public DatabaseLogin(string _server, string _DatabaseName, string _port)
- {
- DatabaseName = _DatabaseName;
- Server = _server;
- Port = _port;
- }
- }
- static class Validators
- {
- public static bool NullStringValidator(string[] _inputs)
- {
- bool _result = false;
- for (int i = 0; i < _inputs.Length; i++)
- {
- if (string.IsNullOrEmpty(_inputs[i]))
- {
- return _result = true;
- }
- }
- return _result;
- }
- }
- AccountLogin accountLogin
- string ConnInfo = "server=" + DatabaseCredentials.Server + ";" +
- "user=" + AccountCredentials.Username + ";" +
- "database=" + DatabaseCredentials.DatabaseName +
- "port=" + DatabaseCredentials.Port +
- "password=" + AccountCredentials.Password;
- StringBuilder
- String.Format("server={0};user={1};...", DatabaseCredentials.Server, AccountCredentials.Username,...);
- $"server={DatabaseCredentials.Server};user={AccountCredentials.Username};..."
- using (DatabaseConnection dc = new DatabaseConnection(...))
- {
- // Use the connection...
- }
- public void Close()
- {
- _connection.Close();
- }
Add Comment
Please, Sign In to add comment