summaryrefslogtreecommitdiff
path: root/kurier/MySQL.cs
blob: 88b4fbfdddb067adf172f80cc4153ba20a62edf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;

namespace kurier
{
    public class MySQL
    {
        private MySqlConnection conn;
        private MySqlCommand cmd;
        private string database;

        public MySQL(string database)
        {
            this.database = database;
            connect();
        }

        public void connect()
        {
            conn = new MySqlConnection((database != "" ? ("Database=" + database + ";") : "")
                + "Data Source=" + getHost() + ";User Id=" + getUser() + ";Password=" + getPass() 
                + ";Port=" + getPort() + ";charset=utf8;");

            conn.Open();
        }

        public void close()
        {
            try
            {
                conn.Close();
                conn = null;
            }
            catch (Exception)
            {
            }
        }

        public bool isOpen()
        {
            return conn != null;
        }

        public static string test()
        {
            try
            {
                MySqlConnection conn = new MySqlConnection("Data Source=" + getHost() + ";User Id=" + getUser() + ";Password=" + getPass()
                + ";Port=" + getPort() + ";charset=utf8;");
                conn.Open();
            }
            catch (MySqlException e)
            {
                return e.Message;
            }
            catch (Exception e)
            {
                return "Prawdopodobnie brakuje Ci dll-ki od MySQL'a.\n\n" + e.Message;
            }
            return "";
        }

        public MySqlDataReader select(string query)
        {
            MySqlCommand comm = new MySqlCommand(query, conn);
            return comm.ExecuteReader();
        }

        public static string getHost() { return Properties.Settings.Default.HOST; }
        public static string getUser() { return Properties.Settings.Default.USER; }
        public static string getPass() { return Properties.Settings.Default.PASS; }
        public static string getPort() { return Properties.Settings.Default.PORT; }
        public static bool getConfigured() { return Properties.Settings.Default.CONFIGURED; }

        internal void backup(string filePath)
        {
            cmd = new MySqlCommand();
            cmd.Connection = conn;
            MySqlBackup backup = new MySqlBackup(cmd);
            backup.ExportInfo.AddCreateDatabase = true;
            backup.ExportToFile(filePath);
        }
    }
}