Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using MySql.Data.MySqlClient;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace E_learning
- {
- class Database
- {
- private MySqlConnection _MySQLconn;
- /**
- * Database - Class constructor
- * stabilize database connection.
- */
- public Database(string _sUser, string _sPassword, string _sDatabase, string _sHost = "locahost")
- {
- // Create DB connection
- _MySQLconn = new MySqlConnection("Server=" + _sHost + ";Database=" + _sDatabase + ";Uid=" + _sUser + ";Pwd="+ _sPassword +";");
- _MySQLconn.Open(); // Open DB connection
- }
- /**
- * query - This functions is used to creates queries
- */
- public DataTable query(string sQuery, Dictionary<string, string> dItems = null)
- {
- MySqlCommand mscSelect = addParam(sQuery, dItems);
- try
- {
- MySqlDataReader mscReader = mscSelect.ExecuteReader();
- DataTable dtData = new DataTable();
- dtData.Load(mscReader);
- return dtData;
- }
- catch (Exception)
- {
- return new DataTable();
- }
- }
- /**
- * addParam - Function that add parameters to the query.
- */
- private MySqlCommand addParam(string sQuery, Dictionary<string, string> dItems)
- {
- MySqlCommand newMSCSelect = new MySqlCommand(sQuery, _MySQLconn); // Create query
- if (dItems == null)
- {
- return newMSCSelect;
- }
- foreach (KeyValuePair<string, string> value in dItems)
- {
- newMSCSelect.Parameters.AddWithValue(value.Key, value.Value);
- }
- return newMSCSelect;
- }
- /**
- * conn - Function that will return the MySQLConnection.
- * When send a parameter it will set a new connection.
- */
- public MySqlConnection conn(MySqlConnection NewMySQLconn = null)
- {
- if (MySqlConnection.ReferenceEquals(_MySQLconn, NewMySQLconn))
- {
- _MySQLconn = NewMySQLconn;
- }
- return _MySQLconn;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement