Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using MySql.Data.MySqlClient;
- using System.Windows.Forms;
- namespace BD_Projekt
- {
- class DBConnect
- {
- private MySqlConnection connection;
- private string server;
- private string database;
- private string uid;
- private string password;
- public DBConnect()
- {
- Initialize();
- }
- private void Initialize()
- {
- server = "localhost";
- database = "sklep";
- uid = "root";
- password = "";
- string ConnectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" +
- "UID=" + uid + ";" + "PASSWORD=" + password + ";";
- connection = new MySqlConnection(ConnectionString);
- }
- private bool OpenConnection()
- {
- try
- {
- connection.Open();
- return true;
- }
- catch(MySqlException ex)
- {
- switch(ex.Number)
- {
- case 0:
- MessageBox.Show("Nie można połączyć się z bazą.");
- break;
- case 1045:
- MessageBox.Show("Zły login lub hasło do bazy.");
- break;
- default:
- MessageBox.Show("Kod błędu: " + ex.Number);
- break;
- }
- return false;
- }
- }
- private bool CloseConnection()
- {
- try
- {
- connection.Close();
- return true;
- }
- catch (MySqlException ex)
- {
- MessageBox.Show(ex.Message);
- return false;
- }
- }
- public List<string>[] Select(string query, List<string> FieldsName)
- {
- List<string>[] list = null;
- if(this.OpenConnection() == true)
- {
- MySqlCommand cmd = new MySqlCommand(query, connection);
- try
- {
- MySqlDataReader dataReader = cmd.ExecuteReader();
- list = new List<string>[dataReader.FieldCount];
- for (int i = 0; i < dataReader.FieldCount; i++)
- list[i] = new List<string>();
- while (dataReader.Read())
- {
- for (int i = 0; i < FieldsName.Count; i++)
- list[i].Add(dataReader[i] + "");
- }
- dataReader.Close();
- }
- catch (MySqlException ex)
- {
- MessageBox.Show(ex.Message);
- }
- this.CloseConnection();
- }
- return list;
- }
- public void Delete(string Table, string Condition)
- {
- string query = "DELETE FROM " + Table;
- if (Condition != null)
- query += " WHERE " + Condition;
- if (this.OpenConnection() == true)
- {
- try
- {
- MySqlCommand cmd = new MySqlCommand(query, connection);
- cmd.ExecuteNonQuery();
- }
- catch (MySqlException ex)
- {
- MessageBox.Show(ex.Message);
- }
- this.CloseConnection();
- }
- }
- public void Update(string Table, List<string> Fields, List<string> Values, string Condition)
- {
- string query = "UPDATE " + Table + " SET ";
- for (int i = 0; i < Fields.Count; i++)
- {
- query += Fields[i]+"="+Values[i];
- if (i != Fields.Count - 1)
- {
- query += ", ";
- }
- }
- if(Condition != null)
- query += " WHERE " + Condition;
- if (this.OpenConnection() == true)
- {
- try
- {
- MySqlCommand cmd = new MySqlCommand(query, connection);
- cmd.ExecuteNonQuery();
- }
- catch (MySqlException ex)
- {
- MessageBox.Show(ex.Message);
- }
- this.CloseConnection();
- }
- }
- public void Insert(string Table, List<string> Fields, List<string> Values)
- {
- string query = "INSERT INTO " + Table + " (";
- for(int i = 0; i<Fields.Count;i++)
- {
- query += Fields[i];
- if(i != Fields.Count-1)
- {
- query += ",";
- }
- }
- query += ") VALUES (";
- for (int i = 0; i < Values.Count; i++)
- {
- query += Values[i];
- if (i != Values.Count - 1)
- {
- query += ",";
- }
- }
- query += ")";
- if (this.OpenConnection() == true)
- {
- try
- {
- MySqlCommand cmd = new MySqlCommand(query, connection);
- cmd.ExecuteNonQuery();
- }
- catch(MySqlException ex)
- {
- MessageBox.Show(ex.Message);
- }
- this.CloseConnection();
- }
- }
- public int Count(string Table)
- {
- string query = "SELECT Count(*) FROM " + Table;
- int Count = -1;
- if (this.OpenConnection() == true)
- {
- try
- {
- MySqlCommand cmd = new MySqlCommand(query, connection);
- Count = int.Parse(cmd.ExecuteScalar() + "");
- }
- catch (MySqlException ex)
- {
- MessageBox.Show(ex.Message);
- }
- this.CloseConnection();
- return Count;
- }
- else
- {
- return Count;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment