dessel191

DBConnect class

Feb 9th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MySql.Data.MySqlClient;
  7. using System.Windows.Forms;
  8.  
  9. namespace BD_Projekt
  10. {
  11.     class DBConnect
  12.     {
  13.         private MySqlConnection connection;
  14.         private string server;
  15.         private string database;
  16.         private string uid;
  17.         private string password;
  18.  
  19.         public DBConnect()
  20.         {
  21.             Initialize();
  22.         }
  23.  
  24.         private void Initialize()
  25.         {
  26.             server      = "localhost";
  27.             database    = "sklep";
  28.             uid         = "root";
  29.             password    = "";
  30.  
  31.             string ConnectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" +
  32.                 "UID=" + uid + ";" + "PASSWORD=" + password + ";";
  33.  
  34.             connection = new MySqlConnection(ConnectionString);
  35.         }
  36.  
  37.         private bool OpenConnection()
  38.         {
  39.             try
  40.             {
  41.                 connection.Open();
  42.                 return true;
  43.             }
  44.             catch(MySqlException ex)
  45.             {
  46.                 switch(ex.Number)
  47.                 {
  48.                     case 0:
  49.                         MessageBox.Show("Nie można połączyć się z bazą.");
  50.                         break;
  51.                     case 1045:
  52.                         MessageBox.Show("Zły login lub hasło do bazy.");
  53.                         break;
  54.                     default:
  55.                         MessageBox.Show("Kod błędu: " + ex.Number);
  56.                         break;
  57.                 }
  58.                 return false;
  59.             }
  60.         }
  61.  
  62.         private bool CloseConnection()
  63.         {
  64.             try
  65.             {
  66.                 connection.Close();
  67.                 return true;
  68.             }
  69.             catch (MySqlException ex)
  70.             {
  71.                 MessageBox.Show(ex.Message);
  72.                 return false;
  73.             }
  74.         }
  75.  
  76.         public List<string>[] Select(string query, List<string> FieldsName)
  77.         {
  78.             List<string>[] list = null;
  79.  
  80.             if(this.OpenConnection() == true)
  81.             {
  82.                 MySqlCommand cmd = new MySqlCommand(query, connection);
  83.  
  84.                 try
  85.                 {
  86.                     MySqlDataReader dataReader = cmd.ExecuteReader();
  87.  
  88.                     list = new List<string>[dataReader.FieldCount];
  89.                     for (int i = 0; i < dataReader.FieldCount; i++)
  90.                         list[i] = new List<string>();
  91.  
  92.                     while (dataReader.Read())
  93.                     {
  94.                         for (int i = 0; i < FieldsName.Count; i++)
  95.                             list[i].Add(dataReader[i] + "");
  96.                     }
  97.  
  98.                     dataReader.Close();
  99.                 }
  100.                 catch (MySqlException ex)
  101.                 {
  102.                     MessageBox.Show(ex.Message);
  103.                 }
  104.  
  105.                 this.CloseConnection();
  106.             }
  107.  
  108.             return list;
  109.         }
  110.  
  111.         public void Delete(string Table, string Condition)
  112.         {
  113.             string query = "DELETE FROM " + Table;
  114.             if (Condition != null)
  115.                 query += " WHERE " + Condition;
  116.  
  117.             if (this.OpenConnection() == true)
  118.             {
  119.                 try
  120.                 {
  121.                     MySqlCommand cmd = new MySqlCommand(query, connection);
  122.                     cmd.ExecuteNonQuery();
  123.                 }
  124.                 catch (MySqlException ex)
  125.                 {
  126.                     MessageBox.Show(ex.Message);
  127.                 }
  128.                 this.CloseConnection();
  129.             }
  130.         }
  131.  
  132.         public void Update(string Table, List<string> Fields, List<string> Values, string Condition)
  133.         {
  134.             string query = "UPDATE " + Table + " SET ";
  135.             for (int i = 0; i < Fields.Count; i++)
  136.             {
  137.                 query += Fields[i]+"="+Values[i];
  138.                 if (i != Fields.Count - 1)
  139.                 {
  140.                     query += ", ";
  141.                 }
  142.             }
  143.             if(Condition != null)
  144.                 query += " WHERE " + Condition;
  145.            
  146.  
  147.  
  148.             if (this.OpenConnection() == true)
  149.             {
  150.                 try
  151.                 {
  152.                     MySqlCommand cmd = new MySqlCommand(query, connection);
  153.                     cmd.ExecuteNonQuery();
  154.                 }
  155.                 catch (MySqlException ex)
  156.                 {
  157.                     MessageBox.Show(ex.Message);
  158.                 }
  159.                 this.CloseConnection();
  160.             }
  161.         }
  162.  
  163.         public void Insert(string Table, List<string> Fields, List<string> Values)
  164.         {
  165.             string query = "INSERT INTO " + Table + " (";
  166.             for(int i = 0; i<Fields.Count;i++)
  167.             {
  168.                 query += Fields[i];
  169.                 if(i != Fields.Count-1)
  170.                 {
  171.                     query += ",";
  172.                 }
  173.             }
  174.             query += ") VALUES (";
  175.             for (int i = 0; i < Values.Count; i++)
  176.             {
  177.                 query += Values[i];
  178.                 if (i != Values.Count - 1)
  179.                 {
  180.                     query += ",";
  181.                 }
  182.             }
  183.             query += ")";
  184.  
  185.             if (this.OpenConnection() == true)
  186.             {
  187.                 try
  188.                 {
  189.                     MySqlCommand cmd = new MySqlCommand(query, connection);
  190.                     cmd.ExecuteNonQuery();
  191.                 }
  192.                 catch(MySqlException ex)
  193.                 {
  194.                     MessageBox.Show(ex.Message);
  195.                 }
  196.                 this.CloseConnection();
  197.             }
  198.         }
  199.  
  200.         public int Count(string Table)
  201.         {
  202.             string query = "SELECT Count(*) FROM " + Table;
  203.             int Count = -1;
  204.  
  205.             if (this.OpenConnection() == true)
  206.             {
  207.                 try
  208.                 {
  209.                     MySqlCommand cmd = new MySqlCommand(query, connection);
  210.                     Count = int.Parse(cmd.ExecuteScalar() + "");
  211.                 }
  212.                 catch (MySqlException ex)
  213.                 {
  214.                     MessageBox.Show(ex.Message);
  215.                 }
  216.  
  217.                 this.CloseConnection();  
  218.                 return Count;
  219.             }
  220.             else
  221.             {
  222.                 return Count;
  223.             }
  224.         }
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment