Guest User

Untitled

a guest
Apr 23rd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MySql.Data.MySqlClient;
  6. using System.Windows.Forms;
  7.  
  8. namespace Shopper
  9. {
  10.     class Database
  11.     {
  12.     private MySqlConnection connection;
  13.     private string server;
  14.     private string database;
  15.     private string uid;
  16.     private string password;
  17.  
  18.     public Database()
  19.     {
  20.         Initialize();
  21.     }
  22.  
  23.     private void Initialize()
  24.     {
  25.         string connectionString;
  26.         connectionString = "SERVER=" + Config.Mysql[0] + ";" + "DATABASE=" + Config.Mysql[3] + ";" + "UID=" + Config.Mysql[1] + ";" + "PASSWORD=" + Config.Mysql[2] + ";";
  27.         connection = new MySqlConnection(connectionString);
  28.     }
  29.  
  30.     private bool OpenConnection()
  31.     {
  32.         try
  33.         {
  34.             connection.Open();
  35.             return true;
  36.         }
  37.         catch (MySqlException ex)
  38.         {
  39.             switch (ex.Number)
  40.             {
  41.                 case 0:
  42.                     MessageBox.Show("Cannot connect to server.  Contact administrator");
  43.                     break;
  44.  
  45.                 case 1045:
  46.                     MessageBox.Show("Mysql Invalid username/password, please try again");
  47.                     break;
  48.             }
  49.             return false;
  50.         }
  51.     }
  52.  
  53.     private bool CloseConnection()
  54.     {
  55.         try
  56.         {
  57.             connection.Close();
  58.             return true;
  59.         }
  60.         catch (MySqlException ex)
  61.         {
  62.             MessageBox.Show(ex.Message);
  63.             return false;
  64.         }
  65.     }
  66.  
  67.     private string FilterInjectionChars(string Input)
  68.     {
  69.         Input = Input.Replace(Convert.ToChar(1), ' ');
  70.         Input = Input.Replace(Convert.ToChar(2), ' ');
  71.         Input = Input.Replace(Convert.ToChar(3), ' ');
  72.         Input = Input.Replace(Convert.ToChar(9), ' ');
  73.         return Input;
  74.     }
  75.  
  76.     private bool Login(string username, string password)
  77.     {
  78.         string query = "SELECT * FROM accounts WHERE Username = '" + FilterInjectionChars(username) + "' and Password = '" + FilterInjectionChars(password) + "'";
  79.         if (this.OpenConnection() == true)
  80.         {
  81.             MySqlCommand cmd = new MySqlCommand(query, connection);
  82.             MySqlDataReader dataReader = cmd.ExecuteReader();
  83.             if (dataReader.NextResult())
  84.             {
  85.                 dataReader.Close();
  86.                 this.CloseConnection();
  87.                 return true;
  88.             }
  89.             else
  90.             {
  91.                 dataReader.Close();
  92.                 this.CloseConnection();
  93.                 return false;
  94.             }
  95.         }
  96.         else
  97.         {
  98.             return false;
  99.         }
  100.     }
  101.  
  102.     public void InsertProduct(string name, int price)
  103.     {
  104.         string query = "INSERT INTO products (Name, Price) VALUES('" + name + "', '" + price + "')";
  105.         if (this.OpenConnection() == true)
  106.         {
  107.             MySqlCommand cmd = new MySqlCommand(query, connection);
  108.             cmd.ExecuteNonQuery();
  109.             this.CloseConnection();
  110.         }
  111.     }
  112.  
  113.     public void UpdateProduct(int id, string name, int price)
  114.     {
  115.         string query = "UPDATE product SET Name='" + name + "', Price='" + price + "' WHERE ID='" + id + "'";
  116.         if (this.OpenConnection() == true)
  117.         {
  118.             MySqlCommand cmd = new MySqlCommand();
  119.             cmd.CommandText = query;
  120.             cmd.Connection = connection;
  121.             cmd.ExecuteNonQuery();
  122.             this.CloseConnection();
  123.         }
  124.     }
  125.  
  126.     public List <string> [] SelectProducts(string query)
  127.     {
  128.         List<string>[] list = new List<string>[3];
  129.         list[0] = new List<string>();
  130.         list[1] = new List<string>();
  131.         list[2] = new List<string>();
  132.  
  133.         if (this.OpenConnection() == true)
  134.         {
  135.             MySqlCommand cmd = new MySqlCommand(query, connection);
  136.             MySqlDataReader dataReader = cmd.ExecuteReader();
  137.             while (dataReader.Read())
  138.             {
  139.                 list[0].Add(dataReader["ID"] + "");
  140.                 list[1].Add(dataReader["Name"] + "");
  141.                 list[2].Add(dataReader["Price"] + "");
  142.             }
  143.             dataReader.Close();
  144.             this.CloseConnection();
  145.             return list;
  146.         }
  147.         else
  148.         {
  149.             return list;
  150.         }
  151.     }
  152.  
  153.     public void DeleteProduct(int id)
  154.     {
  155.         string query = "DELETE FROM product WHERE ID='" + id + "'";
  156.  
  157.         if (this.OpenConnection() == true)
  158.         {
  159.             MySqlCommand cmd = new MySqlCommand(query, connection);
  160.             cmd.ExecuteNonQuery();
  161.             this.CloseConnection();
  162.         }
  163.     }
  164. }
Add Comment
Please, Sign In to add comment