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; } } }