Instances and open a connection - nabrezzelt/HelperLibrary GitHub Wiki
Create and select instance
To create a new connection instance to a MySQL-Database you have to call
MySQLDatabaseManager.CreateInstance();
.
The method will create a new MySQLDatabaseManager
instance.
If no instance name is given a default name will be picked.
If you want to add a second instance, you have to choose a other name e. g. auth
and pass this as argument to CreateInstance();
Connect to database
To open a connection to a database you have to selecet the curret instance. After this you must have to set the login credentials recording you database setup and call the Connect();
method.
Example:
Create a first instance for default DB-Connection:
MySQLDatabaseManager.CreateInstance();
_dbManagerAuth = MySQLDatabaseManager.GetInstance();
_dbManagerDefault.SetConnectionString("localhost", "root", "moritz12", "test-default");
_dbManagerDefault.Connect();
_dbManagerDefault.SQLQueryExecuted += OnSQLQueryExecuted;
Create a second instance for Auth-DB-Connection:
MySQLDatabaseManager.CreateInstance("Auth");
_dbManagerAuth = MySQLDatabaseManager.GetInstance("Auth");
_dbManagerAuth.SetConnectionString("localhost", "root", "moritz12", "test-auth");
_dbManagerAuth.Connect();
_dbManagerAuth.SQLQueryExecuted += OnSQLQueryExecuted;
Test a connection
To test a connection and check if e. g. the login credentials are valid you can use the MySQLDatabaseManager.TestDatabaseConnection()
by giving host, username, password and databasename.
The mehtod will return true if the connection was successful otherwise false.