Advertisement
Guest User

C# database class

a guest
May 21st, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9.  
  10. namespace E_learning
  11. {
  12.     class Database
  13.     {
  14.         private MySqlConnection _MySQLconn;
  15.  
  16.         /**
  17.          * Database - Class constructor
  18.          * stabilize database connection.
  19.          */
  20.         public Database(string _sUser, string _sPassword, string _sDatabase, string _sHost = "locahost")
  21.         {
  22.             // Create DB connection
  23.             _MySQLconn = new MySqlConnection("Server=" + _sHost + ";Database=" + _sDatabase + ";Uid=" + _sUser + ";Pwd="+ _sPassword +";");
  24.             _MySQLconn.Open(); // Open DB connection
  25.         }
  26.  
  27.         /**
  28.          * query - This functions is used to creates queries
  29.          */
  30.         public DataTable query(string sQuery, Dictionary<string, string> dItems = null)
  31.         {
  32.             MySqlCommand mscSelect = addParam(sQuery, dItems);
  33.  
  34.             try
  35.             {
  36.                 MySqlDataReader mscReader = mscSelect.ExecuteReader();
  37.                 DataTable dtData = new DataTable();
  38.                 dtData.Load(mscReader);
  39.                 return dtData;
  40.             }
  41.             catch (Exception)
  42.             {
  43.                 return new DataTable();
  44.             }
  45.         }
  46.  
  47.         /**
  48.          * addParam - Function that add parameters to the query.
  49.          */
  50.         private MySqlCommand addParam(string sQuery, Dictionary<string, string> dItems)
  51.         {
  52.             MySqlCommand newMSCSelect = new MySqlCommand(sQuery, _MySQLconn); // Create query
  53.  
  54.             if (dItems == null)
  55.             {
  56.                 return newMSCSelect;
  57.             }
  58.  
  59.             foreach (KeyValuePair<string, string> value in dItems)
  60.             {
  61.                 newMSCSelect.Parameters.AddWithValue(value.Key, value.Value);
  62.             }
  63.  
  64.             return newMSCSelect;
  65.         }
  66.  
  67.         /**
  68.          * conn - Function that will return the MySQLConnection.
  69.          * When send a parameter it will set a new connection.
  70.          */
  71.         public MySqlConnection conn(MySqlConnection NewMySQLconn = null)
  72.         {
  73.             if (MySqlConnection.ReferenceEquals(_MySQLconn, NewMySQLconn))
  74.             {
  75.                 _MySQLconn = NewMySQLconn;
  76.             }
  77.  
  78.             return _MySQLconn;
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement