/// <summary> /// Pulls all the recipes from the selected table in the database. /// </summary> /// <param name="commandString"></param> /// <param name="recipeBook"></param> public void PullRecipesFromDatabase(string commandString, IRecipeBook recipeBook) //metoda służąca pobieraniu danych //dotyczących przepisów i zapisywaniu { //ich w książce przepisów AbstractRecipeFactory recipeFactory = new StandardRecipeFactory(); //metoda korzysta z fabryki przepisów do konstruowania DataBase.Connection = new MySqlConnection(DataBase.ConnectionString); //umożliwia nawiązanie połączenia DataBase.Command = new MySqlCommand(commandString, (MySqlConnection)DataBase.Connection); //umożliwia przekazanie polecenia using (DataBase.Connection) { using (DataBase.Command) { try { DataBase.Connection.Open(); //otwiera połączenie dataReader = DataBase.Command.ExecuteReader(); //umożliwia odczyt danych z bazy while (dataReader.Read()) { { recipeBook.Recipebook.Add(recipeFactory.CreateRecipe //pobiera dane zapisane w bazie w postaci (dataReader.GetString(0), //stringów i zapisuje je jako obiekt klasy DataBase.Converter.FromStringToListOfIngredients //Recipe (dataReader.GetString(1)), dataReader.GetString(2))); } } } catch (MySqlException) { MessageBox.Show("Failed to connect with the database"); } catch (InvalidOperationException ex) { MessageBox.Show(ex.Message); } } DataBase.Connection.Close();//zamyka połączenie } }
public void PullRecipesFromDatabase(string commandString, IRecipeBook recipeBook) { AbstractRecipeFactory recipeFactory = new StandardRecipeFactory(); DataBase.MYSQLConnection = new MySqlConnection(DataBase.ConnectionString); DataBase.MYSQLCommand = new MySqlCommand(commandString, DataBase.MYSQLConnection); using (DataBase.MYSQLConnection) { using (DataBase.MYSQLCommand) { try { DataBase.MYSQLConnection.Open(); dataReader = DataBase.MYSQLCommand.ExecuteReader(); while (dataReader.Read()) { { recipeBook.Recipebook.Add(recipeFactory.CreateRecipe(dataReader.GetString(0), DataBase.Converter.FromStringToListOfIngredients(dataReader.GetString(1)), dataReader.GetString(2))); } } } catch (MySqlException) { MessageBox.Show("Failed to connect with the database"); } catch (InvalidOperationException ex) { MessageBox.Show(ex.Message); } } DataBase.MYSQLConnection.Close(); } }