-
Notifications
You must be signed in to change notification settings - Fork 13
/
MySql.cs
122 lines (101 loc) · 2.74 KB
/
MySql.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;
using MySql.Data.MySqlClient;
namespace OpenSMO
{
public class MySql
{
private MySqlConnection conn;
public static string Host;
public static string User;
public static string Database;
public static string Password;
public MySql()
{
Initialize();
}
private void Initialize()
{
string connectionString;
connectionString = "server=" + Host + ";" + "database=" +
Database + ";" + "userid=" + User + ";" + "password=" + Password + ";";
conn = new MySqlConnection(connectionString);
}
private bool Open()
{
try
{
conn.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
MainClass.AddLog("MySQL: Cannot connect to server.");
break;
case 1045:
MainClass.AddLog("MySQL: Invalid username/password.");
break;
}
return false;
}
} // end open
private bool Close()
{
try
{
conn.Close();
return true;
}
catch (MySqlException ex)
{
MainClass.AddLog(ex.ToString());
return false;
}
} // end close
public static string AddSlashes(string str)
{
return str.Replace("\\", "\\\\").Replace("'", "\\'");
} // end addslashes
public static Hashtable[] Query(string query)
{
Stopwatch sw = new Stopwatch();
sw.Start();
MySql obj = new MySql();
List<Hashtable> ret = new List<Hashtable>();
if (obj.Open() == true)
{
MySqlCommand cmd = new MySqlCommand(query, obj.conn);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Hashtable row = new Hashtable();
for (int i = 0; i < reader.FieldCount; i++)
{
if (reader[i].GetType() == typeof(Int64))
{
if ((long)reader[i] > int.MaxValue)
row[reader.GetName(i)] = (long)reader[i];
else
row[reader.GetName(i)] = (int)(long)reader[i];
} else
row[reader.GetName(i)] = reader[i];
}//end for
ret.Add(row);
}//end while
reader.Close();
obj.Close();
}//end openconnection
sw.Stop();
if (sw.ElapsedMilliseconds >= 1000)
MainClass.AddLog("SQL Query took very long: " + sw.ElapsedMilliseconds + "ms", true);
return ret.ToArray();
}//end QueryMethod
}//end class
} //end namespace