-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.cs
316 lines (293 loc) · 11.5 KB
/
database.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using System;
using System.Data;
using MySql.Data.MySqlClient;
namespace database
{
public class Test
{
public static string tableName="";
public static void menuDisplay()
{
Console.WriteLine("This menu :m");
Console.WriteLine("Show records :l");
Console.WriteLine("Add new record :i");
Console.WriteLine("Delete record :d");
Console.WriteLine("Modify record :a");
Console.WriteLine("Add column :c");
Console.WriteLine("Delete column :r");
Console.WriteLine("Exit :x");
}
public static void showRecords(ref IDbConnection dbcon)
{
Console.Clear();
IDbCommand dbcmd = dbcon.CreateCommand();
string sql = "SELECT * FROM "+Test.tableName;
dbcmd.CommandText = sql;
IDataReader reader = dbcmd.ExecuteReader();
DataTable schemaTable = reader.GetSchemaTable();
bool firstRow = true;
Console.Clear();
try
{
while (reader.Read())
{
if (firstRow)
{
for (int col = 0; col < reader.FieldCount; col++)
Console.Write("{0,-11}", reader.GetName(col).ToString() + " ");
Console.WriteLine();
for (int col = 0; col < reader.FieldCount; col++)
{
if (reader.GetDataTypeName(col).ToString() == "INT")
Console.Write("number ");
if (reader.GetDataTypeName(col).ToString() == "DOUBLE")
Console.Write("decimal ");
if (reader.GetDataTypeName(col).ToString() == "DATE")
Console.Write("date ");
if (reader.GetDataTypeName(col).ToString() == "TIME")
Console.Write("time ");
if ((reader.GetDataTypeName(col).ToString() == "VARCHAR") || (reader.GetDataTypeName(col).ToString() == "TEXT"))
Console.Write("text ");
}
Console.WriteLine();
}
for (int col = 0; col < reader.FieldCount; col++)
{
Console.Write("{0,-10}", reader[reader.GetName(col).ToString()].ToString());
}
Console.WriteLine();
firstRow = false;
}
Console.WriteLine();
menuDisplay();
reader.Close();
reader = null;
}
catch
{
Console.WriteLine("ERROR: Could not read records");
}
dbcmd.Dispose();
dbcmd = null;
}
public static void addColumn(ref IDbConnection dbcon)
{
Console.WriteLine();
Console.WriteLine("Write name for new column");
string s;
s = Console.ReadLine();
Console.WriteLine("Choose column type");
Console.WriteLine("text :t");
Console.WriteLine("natural numbers :n");
Console.WriteLine("decimal :d");
Console.WriteLine("date :y");
Console.WriteLine("time :h");
ConsoleKeyInfo keyInfo;
bool pick = false;
string s1="";
while (!pick)
{
keyInfo = Console.ReadKey(true);
switch (keyInfo.KeyChar)
{
case 't': s1 = " TEXT"; break;
case 'n': s1 = " INT"; break;
case 'd': s1 = " DOUBLE"; break;
case 'y': s1 = " DATE"; break;
case 'h': s1 = " TIME"; break;
}
if (s1!="") pick = true;
}
IDbCommand dbcmd = dbcon.CreateCommand();
string sql ="ALTER TABLE "+Test.tableName+" ADD " +s+ s1;
dbcmd.CommandText = sql;
try
{
IDataReader reader = dbcmd.ExecuteReader();
reader.Close();
reader = null;
Console.WriteLine("Column added");
}
catch
{
Console.WriteLine("ERROR: Column not added");
}
dbcmd.Dispose();
dbcmd = null;
}
public static void delColumn(ref IDbConnection dbcon)
{
Console.WriteLine();
Console.WriteLine("Write name of column to delete");
string s;
s = Console.ReadLine();
IDbCommand dbcmd = dbcon.CreateCommand();
string sql = "ALTER TABLE "+Test.tableName+" DROP COLUMN " + s ;
dbcmd.CommandText = sql;
try
{
IDataReader reader = dbcmd.ExecuteReader();
reader.Close();
reader = null;
Console.WriteLine("Column deleted");
}
catch
{
Console.WriteLine("ERROR: Column not deleted ! ");
}
dbcmd.Dispose();
dbcmd = null;
}
public static void addRecord(ref IDbConnection dbcon)
{
IDbCommand dbcmd = dbcon.CreateCommand();
string sql = "SELECT * FROM "+Test.tableName;
dbcmd.CommandText = sql;
IDataReader reader = dbcmd.ExecuteReader();
DataTable schemaTable = reader.GetSchemaTable();
reader.Read();
string s = "";
string s1="";
for (int col = 0; col < reader.FieldCount; col++)
{
Console.WriteLine(reader.GetName(col).ToString());
Console.WriteLine("Data type {0}", reader.GetDataTypeName(col).ToString());
Console.WriteLine("Data type {0}", reader.GetFieldType(col).ToString());
s = Console.ReadLine();
s1 = s1 + "'" + s + "',";
}
reader.Close();
reader = null;
s1=s1.Remove(s1.Length - 1);
dbcmd = dbcon.CreateCommand();
sql = "INSERT INTO "+Test.tableName+" VALUES (" + s1 + ")";
dbcmd.CommandText = sql;
try
{
reader = dbcmd.ExecuteReader();
reader.Close();
reader = null;
Console.WriteLine("Record added");
}
catch
{
Console.WriteLine("ERROR: Record not added ! ");
}
dbcmd.Dispose();
dbcmd = null;
}
public static void delRecord(ref IDbConnection dbcon)
{
Console.WriteLine("Enter column name to match value");
string s;
s = Console.ReadLine();
Console.WriteLine("Enter value");
string sql;
sql = Console.ReadLine();
IDbCommand dbcmd = dbcon.CreateCommand();
sql = "DELETE FROM "+Test.tableName+" WHERE "+s+"="+sql;
dbcmd.CommandText = sql;
try
{
IDataReader reader = dbcmd.ExecuteReader();
reader.Close();
reader = null;
Console.WriteLine("Record deleted");
}
catch
{
Console.WriteLine("ERROR: Record not deleted ! ");
}
dbcmd.Dispose();
dbcmd = null;
}
public static void addData(ref IDbConnection dbcon)
{
Console.WriteLine("Enter column name to match recod by value");
string s;
s = Console.ReadLine();
Console.WriteLine("Enter value to match record");
s = s+ " = '" +Console.ReadLine()+ "'";
Console.WriteLine("Enter column name for modification");
string sql= Console.ReadLine();
Console.WriteLine("Enter new value");
sql = "UPDATE "+Test.tableName+" SET " +sql + " = ' " + Console.ReadLine() + "' WHERE "+s;
Console.WriteLine(sql);
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = sql;
try
{
IDataReader reader = dbcmd.ExecuteReader();
reader.Close();
reader = null;
Console.WriteLine("Value updated");
}
catch
{
Console.WriteLine("ERROR: Value not updated ! ");
}
dbcmd.Dispose();
dbcmd = null;
}
public static void Main(string[] args)
{
string readConsole = "";
Console.WriteLine("Enter IP address of SQL server (default localhost)");
readConsole = Console.ReadLine();
if (readConsole == "") readConsole = "localhost";
string connectionString = "Server=" + readConsole + ";";
Console.WriteLine("Enter database name (default test)");
readConsole = Console.ReadLine();
if (readConsole == "") readConsole = "test";
connectionString = connectionString + "Database=" + readConsole + ";";
Console.WriteLine("Enter table name (default test)");
readConsole = Console.ReadLine();
if (readConsole == "") readConsole = "test";
Test.tableName = readConsole;
Console.WriteLine("Enter user ID (default test)");
readConsole = Console.ReadLine();
if (readConsole == "") readConsole = "test";
connectionString = connectionString + "User ID=" + readConsole + ";";
Console.WriteLine("Enter password (default no authentication)");
readConsole = Console.ReadLine();
if (readConsole != "") connectionString = connectionString + "Password=" + readConsole + ";";
connectionString = connectionString + "Pooling=false; Convert Zero Datetime=True; Allow Zero Datetime=True;";
IDbConnection dbcon;
dbcon = new MySqlConnection(connectionString);
try
{
dbcon.Open();
}
catch
{
Console.WriteLine("ERROR: Can not open connection ! ");
Console.ReadLine();
System.Environment.Exit(1);
}
menuDisplay();
ConsoleKeyInfo keyInfo;
while (true)
{
while (keyInfo.KeyChar != 'x')
{
keyInfo = Console.ReadKey(true);
switch (keyInfo.KeyChar)
{
case 'm': menuDisplay(); break;
case 'l': showRecords(ref dbcon); break;
case 'i': addRecord(ref dbcon); break;
case 'd': delRecord(ref dbcon); break;
case 'c':
addColumn(ref dbcon);
break;
case 'r': delColumn(ref dbcon); break;
case 'a':addData(ref dbcon); break;
}
}
if (keyInfo.KeyChar == 'x') break;
}
dbcon.Close();
dbcon = null;
}
}
}