forked from Dragonsdoom/XMLDataTest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseController.cs
More file actions
70 lines (63 loc) · 1.69 KB
/
DatabaseController.cs
File metadata and controls
70 lines (63 loc) · 1.69 KB
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
using System;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
using System.Data;
namespace XMLDataTest
{
public class DatabaseController
{
string connect = "Server = localhost; Database = cs153db; Uid = root; Pwd = \"\";";
MySqlConnection con;
MySqlCommand command;
public DatabaseController()
{
con = new MySqlConnection(connect);
command = new MySqlCommand();
command.Connection = con;
command.CommandText = "SELECT * FROM ORDERS";
}
public void SetTable(string tableName)
{
command.CommandText = "SELECT * FROM " + tableName;
}
public MySqlDataReader GetReader()
{
MySqlDataReader reader;
reader = command.ExecuteReader();
return reader;
}
public bool TryConnectToDatabase()
{
//Try open connection
try
{
con.Open();
}
catch (MySqlException e)
{
Console.WriteLine("Error: " + e.Message);
return false;
}
#if DEBUG
Console.WriteLine("Made the connection to the MySQL database.");
#endif
return true;
}
public bool TryCloseConnection()
{
try
{
if (con.State != System.Data.ConnectionState.Closed)
{
con.Close();
}
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
return false;
}
return true;
}
}
}