-
Notifications
You must be signed in to change notification settings - Fork 0
/
DB.cs
146 lines (133 loc) · 4.5 KB
/
DB.cs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace DXApplication2
{
class DB
{
List<string> tables = new List<string>();
List<string> views = new List<string>();
DataTable columnsOfTable = new DataTable();
string connStr = "SERVER=localhost;DATABASE=tbname;UID=root;PASSWORD=;";
string nameColumn = "Tables";
public DB()
{
using (var conn = new MySqlConnection(connStr))
{
try
{
conn.Open();
tables = MakeListTables(conn);
views = MakeListViews(conn);
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator");
break;
case 1045:
MessageBox.Show("Invalid username/password, please try again");
break;
}
}
finally
{
if (conn != null)
conn.Close();
}
}
}
public List<string> Tables
{
get
{
return tables;
}
}
public List<string> Views
{
get
{
return views;
}
}
private DataTable ShowTables(MySqlConnection conn)
{
DataTable table = new DataTable();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter();
string sql = "SHOW FULL TABLES WHERE Table_Type != 'VIEW'";
dataAdapter.SelectCommand = new MySqlCommand(sql, conn);
dataAdapter.Fill(table);
return table;
}
private List<string> MakeListTables(MySqlConnection conn)
{
List<string> list = new List<string>();
DataTable table = ShowTables(conn);
foreach (DataRow row in table.Rows)
foreach (DataColumn column in table.Columns)
if (column.ToString() == nameColumn)
list.Add(row[column].ToString());
return list;
}
private DataTable ShowViews(MySqlConnection conn)
{
DataTable table = new DataTable();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter();
string sql = "SHOW FULL TABLES WHERE Table_Type = 'VIEW'";
dataAdapter.SelectCommand = new MySqlCommand(sql, conn);
dataAdapter.Fill(table);
return table;
}
private List<string> MakeListViews(MySqlConnection conn)
{
List<string> list = new List<string>();
DataTable table = ShowViews(conn);
foreach (DataRow row in table.Rows)
foreach (DataColumn column in table.Columns)
if (column.ToString() == nameColumn)
list.Add(row[column].ToString());
return list;
}
public DataTable GetColumnsOfTable(string tableName)
{
DataTable table = new DataTable();
using (var conn = new MySqlConnection(connStr))
{
try
{
conn.Open();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter();
string sql = $"SHOW COLUMNS FROM {tableName}";
dataAdapter.SelectCommand = new MySqlCommand(sql, conn);
dataAdapter.Fill(table);
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator");
break;
case 1045:
MessageBox.Show("Invalid username/password, please try again");
break;
}
}
finally
{
if (conn != null)
conn.Close();
}
}
return table;
}
}
}