Exemple #1
0
        private void Recipe_view_screen_Load(object sender, EventArgs e)
        {
            //Display the title that is being selected from the viewlist in the previous screen.
            titleOfSelectedFood.Text = Listof_items_screen.foodTitle;
            //titleOfSelectedFood.SelectionAlignment = HorizontalAlignment.Center;//Align Title text in the center.

            //Create a list of this object to be filled with data from the database.
            List <Database_Items_Class> itemsFromDb = new List <Database_Items_Class>();

            //Search database Title and put all items with that title into a list.
            string query = "SELECT * from recipies WHERE Title='" + Listof_items_screen.foodTitle + "'";

            using (SQLiteCommand cmd = new SQLiteCommand(query, databaseObject.myConnection))
            {
                databaseObject.myConnection.Open();
                using (SQLiteDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Database_Items_Class item = new Database_Items_Class(); //Create the object and fill it's variables with database data.
                        item.Title       = reader.GetString(0);
                        item.Category    = reader.GetString(1);
                        item.Ingredients = reader.GetString(2);
                        //ingredientsTextBox.Text = reader.GetString(2);
                        item.Description = reader.GetString(3);
                        itemsFromDb.Add(item);

                        //Display the corresponding database cells to the text boxes.
                        ingredientsTextBox.Text = item.Ingredients;
                        descriptionTextBox.Text = item.Description;
                    }
                }
                databaseObject.myConnection.Close();
            }
        }
Exemple #2
0
        public static void exportDB()
        {
            Database_Items_Class databaseObject = new Database_Items_Class(); //sqlite object

            Directory.CreateDirectory(folder_Creation);
            SQLiteDataReader reader;
            string           query        = "SELECT * FROM recipies WHERE Title='" + Listof_items_screen.foodTitle + "'";
            string           strDelimiter = "\n";
            string           strFilePath  = @"C:\Recipies\" + Listof_items_screen.foodTitle + "_recipe.txt";;

            databaseObject.OpenConnection();
            using (reader = new SQLiteCommand(query, databaseObject.myConnection).ExecuteReader())
            {
                if (reader.HasRows)
                {
                    StringBuilder sb    = new StringBuilder();
                    Object[]      items = new Object[reader.FieldCount];
                    List <Database_Items_Class> itemsFromDb = new List <Database_Items_Class>();
                    Database_Items_Class        item        = new Database_Items_Class(); //Create the object and fill it's variables with database data.

                    while (reader.Read())
                    {
                        reader.GetValues(items);
                        item.Title       = reader.GetString(0);
                        item.Category    = reader.GetString(1);
                        item.Ingredients = reader.GetString(2);
                        //ingredientsTextBox.Text = reader.GetString(2);
                        item.Description = reader.GetString(3);
                        itemsFromDb.Add(item);
                        //Console.WriteLine(item.Ingredients);
                        // Using two sb.Append is better because if you
                        // concat the two strings it will first build a new string
                        // and then discard it after its use.
                        //sb.Append(strDelimiter);
                        //sb.Append(item.ToString());
                    }

                    //Write into txt file
                    sb.Append("Title: " + item.Title + "\n");
                    sb.Append("===========================================================================\n");
                    sb.Append("Ingredients: \n\n" + item.Ingredients + "\n");
                    sb.Append("===========================================================================\n");
                    sb.Append("Description: \n\n" + item.Description + "\n");

                    File.WriteAllText(strFilePath, sb.ToString());
                }
            }
            databaseObject.CloseConnection();
        }