-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBConnector.cs
59 lines (57 loc) · 2.13 KB
/
DBConnector.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
using System.Collections.Generic;
using System.Data;
using MySql.Data.MySqlClient;
namespace LoginFun
{
public class DbConnector
{
static string server = "localhost";
static string db = "mydb"; //Change to your schema name
static string port = "3306"; //Potentially 8889
static string user = "root";
static string pass = "root";
internal static IDbConnection Connection {
get {
return new MySqlConnection($"Server={server};Port={port};Database={db};UserID={user};Password={pass};SslMode=None");
}
}
//This method runs a query and stores the response in a list of dictionary records
public static List<Dictionary<string, object>> Query(string queryString)
{
using(IDbConnection dbConnection = Connection)
{
using(IDbCommand command = dbConnection.CreateCommand())
{
command.CommandText = queryString;
dbConnection.Open();
var result = new List<Dictionary<string, object>>();
using(IDataReader rdr = command.ExecuteReader())
{
while(rdr.Read())
{
var dict = new Dictionary<string, object>();
for( int i = 0; i < rdr.FieldCount; i++ ) {
dict.Add(rdr.GetName(i), rdr.GetValue(i));
}
result.Add(dict);
}
return result;
}
}
}
}
//This method run a query and returns no values
public static void Execute(string queryString)
{
using (IDbConnection dbConnection = Connection)
{
using(IDbCommand command = dbConnection.CreateCommand())
{
command.CommandText = queryString;
dbConnection.Open();
command.ExecuteNonQuery();
}
}
}
}
}