EHDSeven

Pluton Mysql Plugin

Jan 14th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1. namespace Pluton
  2. {
  3.     using System;
  4.     using MySql.Data.MySqlClient;
  5.  
  6.     public class Storage
  7.     {
  8.         private MySqlConnection Conn;
  9.         private static Storage instance;
  10.  
  11.         public static Storage GetInstance()
  12.         {
  13.             if (instance == null)
  14.                 instance = new Storage();
  15.             return instance;
  16.         }
  17.  
  18.         public MySqlConnection Connect(string host, string user, string db, string port, string pass)
  19.         {
  20.             MySqlConnection connection;
  21.             try
  22.             {
  23.                 connection = new MySqlConnection(String.Format("server={0};user={1};database={2};port={3};password={4};", host, user, db, port, pass));
  24.                 connection.Open();
  25.                 Logger.LogDebug("MySql Connection Opened.");
  26.                 return connection;
  27.             }
  28.             catch (Exception ex)
  29.             {
  30.                 Logger.LogError("Could not Connect to MySQL Server");
  31.                 Logger.LogException(ex);
  32.                 return new MySqlConnection();
  33.             }
  34.         }
  35.  
  36.         public object Query(string query, MySqlConnection Conn)
  37.         {
  38.             MySqlDataReader dr;
  39.  
  40.             try
  41.             {
  42.                 MySqlCommand cmd = new MySqlCommand(query, Conn);
  43.                 dr = cmd.ExecuteReader();
  44.             }
  45.             catch (Exception ex)
  46.             {
  47.                 Logger.LogError("MySql Query Error");
  48.                 Logger.LogException(ex);
  49.                 return false;
  50.             }
  51.             return dr;
  52.         }
  53.  
  54.         public object QueryCount(string query, MySqlConnection Conn)
  55.         {
  56.             object data;
  57.             try
  58.             {
  59.                 MySqlCommand cmd = new MySqlCommand(query, Conn);
  60.                 data = cmd.ExecuteScalar();
  61.                 if (data != null)
  62.                     int d = Convert.ToInt32(data);
  63.             }
  64.             catch (Exception ex)
  65.             {
  66.                 Logger.LogError("MySql Query Count Error");
  67.                 Logger.LogException(ex);
  68.                 return false;
  69.             }
  70.             return data;
  71.         }
  72.  
  73.         public bool Insert(string query, MySqlConnection Conn)
  74.         {
  75.             int inst;
  76.             try
  77.             {
  78.                 MySqlCommand cmd = new MySqlCommand(query, Conn);
  79.                 inst = cmd.ExecuteNonQuery();
  80.             }
  81.             catch (Exception ex)
  82.             {
  83.                 Logger.LogError("MySql Insert Error");
  84.                 Logger.LogException(ex);
  85.                 return false;
  86.             }
  87.            
  88.             if (inst > 0)
  89.                 return true;
  90.             else
  91.                 return false;
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment