From 10f75f936e1322ea1e328799cfba08229d667a78 Mon Sep 17 00:00:00 2001 From: Adam Frisby Date: Fri, 4 May 2007 03:25:20 +0000 Subject: Committing OpenGrid.Framework.Data and MySql Adaptor - not in functional state yet, posted for reference and future use. --- OpenGrid.Framework.Data.MySQL/MySQLManager.cs | 84 +++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 OpenGrid.Framework.Data.MySQL/MySQLManager.cs (limited to 'OpenGrid.Framework.Data.MySQL/MySQLManager.cs') 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 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Data; + +// MySQL Native +using MySql; +using MySql.Data; +using MySql.Data.Types; +using MySql.Data.MySqlClient; + +namespace OpenGrid.Framework.Data.MySQL +{ + class MySQLManager + { + IDbConnection dbcon; + + /// + /// Initialises and creates a new MySQL connection and maintains it. + /// + /// The MySQL server being connected to + /// The name of the MySQL database being used + /// The username logging into the database + /// The password for the user logging in + /// Whether to use connection pooling or not, can be one of the following: 'yes', 'true', 'no' or 'false', if unsure use 'false'. + public MySQLManager(string hostname, string database, string username, string password, string cpooling) + { + try + { + string connectionString = "Server=" + hostname + ";Database=" + database + ";User ID=" + username + ";Password=" + password + ";Pooling=" + cpooling + ";"; + dbcon = new MySqlConnection(connectionString); + + dbcon.Open(); + } + catch (Exception e) + { + throw new Exception("Error initialising MySql Database: " + e.ToString()); + } + } + + /// + /// Shuts down the database connection + /// + public void Close() + { + dbcon.Close(); + dbcon = null; + } + + /// + /// Runs a query with protection against SQL Injection by using parameterised input. + /// + /// The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y + /// The parameters - index so that @y is indexed as 'y' + /// A MySQL DB Command + public IDbCommand Query(string sql, Dictionary parameters) + { + MySqlCommand dbcommand = (MySqlCommand)dbcon.CreateCommand(); + dbcommand.CommandText = sql; + foreach (KeyValuePair param in parameters) + { + dbcommand.Parameters.Add(param.Key, param.Value); + } + + return (IDbCommand)dbcommand; + } + + public SimProfileData getRow(IDataReader reader) + { + SimProfileData retval = new SimProfileData(); + + if (reader.Read()) + { + //retval.regionDataURI = reader["regionDataURI"]; + + } + else + { + return null; + } + return retval; + } + } +} -- cgit v1.1