-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_access.cs
More file actions
107 lines (95 loc) · 2.57 KB
/
Copy pathsql_access.cs
File metadata and controls
107 lines (95 loc) · 2.57 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
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
using System;
using System.Collections.Generic;
using System.Data;
using MySql.Data.MySqlClient;
namespace Notification_Squad_DNC
{
class Sql_access
{
readonly MySqlConnection SQLConn;
public readonly List<string> statusList;
public readonly List<User> userList;
public Sql_access(string cs)
{
statusList = new List<string>();
userList = new List<User>();
SQLConn = new MySqlConnection(cs);
try
{
SQLConn.Open();
Console.WriteLine(SQLConn.ServerVersion.ToString());
}
catch
{
throw new Exception("Database Connection Error");
}
}
public void ReopenConnections()
{
try
{
SQLConn.Close();
SQLConn.Open();
}
catch
{
throw new Exception("Database Connection Error");
}
}
//update the status of the user on the database.
public void SetStatus(string name, string status_name)
{
string sqlstatement = String.Format("update notification_squad.user_names set notification_squad.user_names.status_id = (Select status_id from status where status_name = '{1}') where name = '{0}';", name, status_name);
Console.WriteLine(sqlstatement);
ReopenConnections();
MySqlCommand cmd = new MySqlCommand(sqlstatement, SQLConn);
cmd.ExecuteNonQuery();
SQLConn.Close();
}
//get the status table, used to populate names of buttons
public void GetStatusAll()
{
string sqlstatement = "SELECT * FROM status;";
ReopenConnections();
MySqlCommand cmd = new MySqlCommand(sqlstatement, SQLConn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
ReadSingleRow((IDataRecord)rdr);
}
// Call Close when done reading.
rdr.Close();
}
private void ReadSingleRow(IDataRecord record)
{
Console.WriteLine(String.Format("{0}, {1}", record[0], record[1]));
statusList.Add(record[1].ToString());
}
public void GetUserStatusAll()
{
ReopenConnections();
userList.Clear();
string sqlstatement = "SELECT user_names.name,status.status_name FROM notification_squad.user_names INNER JOIN notification_squad.status ON user_names.status_id = status.status_id ORDER BY user_names.name ASC;";
MySqlCommand cmd = new MySqlCommand(sqlstatement, SQLConn);
MySqlDataReader rdr = cmd.ExecuteReader();
int i = 0;
while (rdr.Read())
{
Console.WriteLine("{0} {1}", rdr.GetString(0), rdr.GetString(1));
userList.Add(new User(rdr.GetString(0), rdr.GetString(1)));
i++;
}
rdr.Close();
}
}
struct User
{
public string name;
public string status;
public User(string Name, string Status)
{
name = Name;
status = Status;
}
}
}