Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using MySql.Data;
- using MySql.Data.MySqlClient;
- public class MySQLHandler
- {
- public MySQLHandler()
- {
- }
- private MySqlConnection connection = null;
- public MySqlConnection Connection
- {
- get { return connection; }
- }
- private static MySQLHandler _instance = null;
- public static MySQLHandler Instance()
- {
- if (_instance == null)
- _instance = new MySQLHandler();
- return _instance;
- }
- public bool IsConnect()
- {
- bool result = true;
- try {
- if (Connection == null)
- {
- if (String.IsNullOrEmpty(ServerSettings.SQL_AUTHDB))
- result = false;
- MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
- conn_string.Server = ServerSettings.SQL_HOST;
- conn_string.UserID = ServerSettings.SQL_USER;
- conn_string.Password = ServerSettings.SQL_PASS;
- conn_string.Database = ServerSettings.SQL_AUTHDB;
- connection = new MySqlConnection(conn_string.ToString());
- Log("Connecting to MySQL: " + ServerSettings.SQL_HOST + ", " + ServerSettings.SQL_USER);
- connection.StateChange += Connection_StateChange;
- connection.Open();
- result = true;
- }
- }
- catch (Exception e)
- {
- Log(e.Message + "\r\n" + e.StackTrace);
- }
- return result;
- }
- private void Connection_StateChange(object sender, System.Data.StateChangeEventArgs e)
- {
- if (e.CurrentState == System.Data.ConnectionState.Open)
- {
- Log("Connected to MySQL: " + ServerSettings.SQL_HOST);
- }
- if (e.CurrentState == System.Data.ConnectionState.Closed)
- {
- //Log("Closed Connection to MySQL: " + ServerSettings.SQL_HOST);
- }
- //throw new NotImplementedException();
- }
- public void Close()
- {
- try {
- connection.Close();
- }
- catch (Exception e)
- {
- Log(e.Message + "\r\n" + e.StackTrace);
- }
- }
- private void Log(string message)
- {
- Console.WriteLine(message);
- WriteToLog.Log(message, "LOG");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement