Advertisement
HolyFot

MySQL Handler

Sep 1st, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MySql.Data;
  6. using MySql.Data.MySqlClient;
  7.  
  8. public class MySQLHandler
  9. {
  10.     public MySQLHandler()
  11.     {
  12.     }
  13.  
  14.     private MySqlConnection connection = null;
  15.     public MySqlConnection Connection
  16.     {
  17.         get { return connection; }
  18.     }
  19.     private static MySQLHandler _instance = null;
  20.     public static MySQLHandler Instance()
  21.     {
  22.         if (_instance == null)
  23.             _instance = new MySQLHandler();
  24.        return _instance;
  25.     }
  26.  
  27.     public bool IsConnect()
  28.     {
  29.         bool result = true;
  30.         try {
  31.             if (Connection == null)
  32.             {
  33.                 if (String.IsNullOrEmpty(ServerSettings.SQL_AUTHDB))
  34.                     result = false;
  35.  
  36.                 MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
  37.                 conn_string.Server = ServerSettings.SQL_HOST;
  38.                 conn_string.UserID = ServerSettings.SQL_USER;
  39.                 conn_string.Password = ServerSettings.SQL_PASS;
  40.                 conn_string.Database = ServerSettings.SQL_AUTHDB;
  41.                 connection = new MySqlConnection(conn_string.ToString());
  42.  
  43.                 Log("Connecting to MySQL: " + ServerSettings.SQL_HOST + ", " + ServerSettings.SQL_USER);
  44.                 connection.StateChange += Connection_StateChange;
  45.  
  46.                 connection.Open();
  47.                
  48.                 result = true;
  49.             }
  50.         }
  51.         catch (Exception e)
  52.         {
  53.             Log(e.Message + "\r\n" + e.StackTrace);
  54.         }
  55.  
  56.         return result;
  57.     }
  58.  
  59.     private void Connection_StateChange(object sender, System.Data.StateChangeEventArgs e)
  60.     {
  61.         if (e.CurrentState == System.Data.ConnectionState.Open)
  62.         {
  63.             Log("Connected to MySQL: " + ServerSettings.SQL_HOST);
  64.         }
  65.         if (e.CurrentState == System.Data.ConnectionState.Closed)
  66.         {
  67.             //Log("Closed Connection to MySQL: " + ServerSettings.SQL_HOST);
  68.         }
  69.         //throw new NotImplementedException();
  70.     }
  71.  
  72.     public void Close()
  73.     {
  74.         try {
  75.             connection.Close();
  76.         }
  77.         catch (Exception e)
  78.         {
  79.             Log(e.Message + "\r\n" + e.StackTrace);
  80.         }
  81.     }
  82.  
  83.     private void Log(string message)
  84.     {
  85.         Console.WriteLine(message);
  86.         WriteToLog.Log(message, "LOG");
  87.     }
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement