Advertisement
seangraham44

MySQL DataAccess

May 18th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.48 KB | None | 0 0
  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace TicketManagementSystem
  11. {
  12.     public class DataAccess
  13.     {
  14.         #region Constructor
  15.  
  16.         public DataAccess()
  17.         {
  18.             ConfigDB = ConfigurationManager.AppSettings.Get("dbSchema");
  19.             ConfigServer = ConfigurationManager.AppSettings.Get("dbServer");
  20.             ConfigDBUser = ConfigurationManager.AppSettings.Get("dbUsername");
  21.             ConfigDBPassword = ConfigurationManager.AppSettings.Get("dbPassword");
  22.             ConfigDBPort = "3306";
  23.         }
  24.  
  25.         public DataAccess(string db, string server, string user, string pw)
  26.         {
  27.             ConfigDB = db ;
  28.             ConfigServer = server;
  29.             ConfigDBUser = user ;
  30.             ConfigDBPassword = pw ;
  31.             ConfigDBPort = "3306";
  32.         }
  33.         #endregion
  34.  
  35.         #region Private
  36.         private string Server { get; set; }
  37.         private string ConfigServer { get; set; }
  38.         private string ConfigDBUser { get; set; }
  39.         private string ConfigDBPassword { get; set; }
  40.         private string ConfigDB { get; set; }
  41.         private string ConfigDBPort { get; set; }
  42.         #endregion
  43.  
  44.         #region Exposed Methods
  45.  
  46.         public string connectionString
  47.         {
  48.             get
  49.             {
  50.                 return string.Format("SERVER={0};UID={1};PASSWORD={2};DATABASE={3};PORT={4}; respect binary flags=false;", ConfigServer, ConfigDBUser, ConfigDBPassword, ConfigDB, ConfigDBPort);
  51.  
  52.             }
  53.         }
  54.  
  55.         public MySqlConnection Connection
  56.         {
  57.             get
  58.             {
  59.                 return new MySqlConnection(connectionString);
  60.             }
  61.         }
  62.  
  63.         public DataTable FetchData(string sql)
  64.         {
  65.             return FetchData(sql, null);
  66.         }
  67.  
  68.         public DataTable FetchData(string sql, List<MySqlParameter> param)
  69.         {
  70.             using (MySqlConnection conn = Connection)
  71.             {
  72.                 MySqlCommand cmd = new MySqlCommand(sql, conn);
  73.  
  74.                 if (param != null)
  75.                 {
  76.                     foreach (MySqlParameter par in param)
  77.                     {
  78.                         cmd.Parameters.AddWithValue(par.ParameterName, par.Value);
  79.                     }
  80.                 }
  81.  
  82.                 cmd.Connection.Open();
  83.  
  84.                 MySqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  85.  
  86.                 DataTable dt = new DataTable();
  87.  
  88.                 dt.Load(rdr);
  89.  
  90.                 cmd.Connection.Close();
  91.  
  92.                 return dt;
  93.             }
  94.         }
  95.  
  96.         public void DBOps(string sql)
  97.         {
  98.             DBOps(sql, null);
  99.         }
  100.  
  101.         public void DBOps(string sql, List<MySqlParameter> param)
  102.         {
  103.             using (MySqlConnection conn = Connection)
  104.             {
  105.                 MySqlCommand cmd = new MySqlCommand(sql, conn);
  106.  
  107.                 if (param != null)
  108.                 {
  109.                     foreach (MySqlParameter par in param)
  110.                     {
  111.                         cmd.Parameters.AddWithValue(par.ParameterName, par.Value);
  112.                     }
  113.                 }
  114.  
  115.                 conn.Open();
  116.                 cmd.ExecuteNonQuery();
  117.                 cmd.Connection.Close();
  118.                 conn.Close();
  119.             }
  120.         }
  121.         #endregion
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement