using MySql.Data.MySqlClient; namespace Nevyn.Classes { public class Mysql { private MySqlConnection connection; private string server; private string database; private string uid; private string password; //Constructor public Mysql() { Initialize(); } //Initialize values private void Initialize() { server = "192.168.1.108"; database = "Nevyn"; uid = "nevyn"; password = "3edc4RFV"; string connectionString; connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";"; connection = new MySqlConnection(connectionString); } //open connection to database public bool OpenConnection() { try { if (connection.State != System.Data.ConnectionState.Open) connection.Open(); return true; } catch (MySqlException ex) { //When handling errors, you can your application's response based //on the error number. //The two most common error numbers when connecting are as follows: //0: Cannot connect to server. //1045: Invalid user name and/or password. switch (ex.Number) { case 0: Console.Write("Cannot connect to server. Contact administrator"); break; case 1045: Console.Write("Invalid username/password, please try again"); break; } return false; } } //Close connection public bool CloseConnection() { try { connection.Close(); return true; } catch (MySqlException ex) { Console.Write(ex.Message); return false; } } public Models.Wallet SelectWallet() { string query = "SELECT * FROM tblWallet"; //Create a list to store the result Models.Wallet wallet = new Models.Wallet(); //Open connection if (this.OpenConnection() == true) { //Create Command MySqlCommand cmd = new MySqlCommand(query, connection); //Create a data reader and Execute the command MySqlDataReader dataReader = cmd.ExecuteReader(); //Read the data and store them in the list while (dataReader.Read()) { wallet.Id = (int)dataReader["idtblWallet"]; wallet.Kort = (int)dataReader["Kort"]; wallet.Buffert = (int)dataReader["Buffert"]; wallet.Spara = (int)dataReader["Spara"]; } //close Data Reader dataReader.Close(); //close Connection this.CloseConnection(); //return list to be displayed return wallet; } else { return wallet; } } public List getBudgetItems(int ID) { string query = "SELECT * FROM tblBudgetItems WHERE category='" + ID + "'"; //Create a list to store the result List budgetItems = new List(); //Open connection if (this.OpenConnection() == true) { //Create Command MySqlCommand cmd = new MySqlCommand(query, connection); //Create a data reader and Execute the command MySqlDataReader dataReader = cmd.ExecuteReader(); //Read the data and store them in the list while (dataReader.Read()) { Models.BudgetItem item = new Models.BudgetItem(); item.ID = (int)dataReader["idtblBudgetItems"]; item.Name = (string)dataReader["Name"]; item.Amount = (int)dataReader["Amount"]; item.Description = (string)dataReader["Description"]; item.Month = (int)dataReader["Month"]; item.Year = (int)dataReader["Year"]; //item.User = (int)dataReader["UserID"]; budgetItems.Add(item); } //close Data Reader dataReader.Close(); //close Connection //this.CloseConnection(); //return list to be displayed return budgetItems; } else { return budgetItems; } } public List getBudgetCategories() { string query = "SELECT * FROM tblCategories"; //Create a list to store the result List categoryList = new List(); //Open connection if (this.OpenConnection() == true) { //Create Command MySqlCommand cmd = new MySqlCommand(query, connection); //Create a data reader and Execute the command MySqlDataReader dataReader = cmd.ExecuteReader(); //Read the data and store them in the list while (dataReader.Read()) { Models.Category category = new Models.Category(); category.ID = (int)dataReader["idtblCategories"]; category.Name = (string)dataReader["Name"]; category.parent = (int)dataReader["parent"]; categoryList.Add(category); } //close Data Reader dataReader.Close(); for (var i = 0; i < categoryList.Count(); i++) { categoryList[i].Items = this.getBudgetItems(categoryList[i].ID); } //close Connection this.CloseConnection(); //return list to be displayed return categoryList; } else { return categoryList; } } } }