diff options
Diffstat (limited to 'OpenGrid.Framework.Data.MySQL/MySQLManager.cs')
-rw-r--r-- | OpenGrid.Framework.Data.MySQL/MySQLManager.cs | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/OpenGrid.Framework.Data.MySQL/MySQLManager.cs b/OpenGrid.Framework.Data.MySQL/MySQLManager.cs new file mode 100644 index 0000000..30ad314 --- /dev/null +++ b/OpenGrid.Framework.Data.MySQL/MySQLManager.cs | |||
@@ -0,0 +1,84 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Text; | ||
4 | using System.Data; | ||
5 | |||
6 | // MySQL Native | ||
7 | using MySql; | ||
8 | using MySql.Data; | ||
9 | using MySql.Data.Types; | ||
10 | using MySql.Data.MySqlClient; | ||
11 | |||
12 | namespace OpenGrid.Framework.Data.MySQL | ||
13 | { | ||
14 | class MySQLManager | ||
15 | { | ||
16 | IDbConnection dbcon; | ||
17 | |||
18 | /// <summary> | ||
19 | /// Initialises and creates a new MySQL connection and maintains it. | ||
20 | /// </summary> | ||
21 | /// <param name="hostname">The MySQL server being connected to</param> | ||
22 | /// <param name="database">The name of the MySQL database being used</param> | ||
23 | /// <param name="username">The username logging into the database</param> | ||
24 | /// <param name="password">The password for the user logging in</param> | ||
25 | /// <param name="cpooling">Whether to use connection pooling or not, can be one of the following: 'yes', 'true', 'no' or 'false', if unsure use 'false'.</param> | ||
26 | public MySQLManager(string hostname, string database, string username, string password, string cpooling) | ||
27 | { | ||
28 | try | ||
29 | { | ||
30 | string connectionString = "Server=" + hostname + ";Database=" + database + ";User ID=" + username + ";Password=" + password + ";Pooling=" + cpooling + ";"; | ||
31 | dbcon = new MySqlConnection(connectionString); | ||
32 | |||
33 | dbcon.Open(); | ||
34 | } | ||
35 | catch (Exception e) | ||
36 | { | ||
37 | throw new Exception("Error initialising MySql Database: " + e.ToString()); | ||
38 | } | ||
39 | } | ||
40 | |||
41 | /// <summary> | ||
42 | /// Shuts down the database connection | ||
43 | /// </summary> | ||
44 | public void Close() | ||
45 | { | ||
46 | dbcon.Close(); | ||
47 | dbcon = null; | ||
48 | } | ||
49 | |||
50 | /// <summary> | ||
51 | /// Runs a query with protection against SQL Injection by using parameterised input. | ||
52 | /// </summary> | ||
53 | /// <param name="sql">The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y</param> | ||
54 | /// <param name="parameters">The parameters - index so that @y is indexed as 'y'</param> | ||
55 | /// <returns>A MySQL DB Command</returns> | ||
56 | public IDbCommand Query(string sql, Dictionary<string, string> parameters) | ||
57 | { | ||
58 | MySqlCommand dbcommand = (MySqlCommand)dbcon.CreateCommand(); | ||
59 | dbcommand.CommandText = sql; | ||
60 | foreach (KeyValuePair<string, string> param in parameters) | ||
61 | { | ||
62 | dbcommand.Parameters.Add(param.Key, param.Value); | ||
63 | } | ||
64 | |||
65 | return (IDbCommand)dbcommand; | ||
66 | } | ||
67 | |||
68 | public SimProfileData getRow(IDataReader reader) | ||
69 | { | ||
70 | SimProfileData retval = new SimProfileData(); | ||
71 | |||
72 | if (reader.Read()) | ||
73 | { | ||
74 | //retval.regionDataURI = reader["regionDataURI"]; | ||
75 | |||
76 | } | ||
77 | else | ||
78 | { | ||
79 | return null; | ||
80 | } | ||
81 | return retval; | ||
82 | } | ||
83 | } | ||
84 | } | ||