コード例 #1
0
        public static List <ViewModels.MealListingVM> GetMealsByName(string searchString)
        {
            List <ViewModels.MealListingVM> resultList = new List <ViewModels.MealListingVM>();

            using (SQLiteConnection con = new SQLiteConnection(conString))
            {
                con.Open();

                // This SQL search query could be improved a lot.
                string query = "SELECT MealID, MealName, MealIngredients FROM Meals WHERE MealName LIKE '%" + searchString + "%'" +
                               " OR MealIngredients LIKE '%" + searchString + "%'";

                using (SQLiteCommand cmd = new SQLiteCommand(query, con))
                {
                    using (SQLiteDataReader dataReader = cmd.ExecuteReader())
                    {
                        if (dataReader.HasRows)
                        {
                            while (dataReader.Read())
                            {
                                ViewModels.MealListingVM meal = new ViewModels.MealListingVM();

                                meal.MealID          = Convert.ToInt32(dataReader["MealID"]);
                                meal.MealName        = Convert.ToString(dataReader["MealName"]);
                                meal.MealIngredients = Convert.ToString(dataReader["MealIngredients"]);
                                // Add meals to result list which we send to Controller
                                resultList.Add(meal);
                            }
                        }
                    }
                }
                return(resultList);
            }
        }
コード例 #2
0
        public static List <ViewModels.MealListingVM> GetAllMeals()
        {
            SetupDB();
            // This list of meals gets filled up with data from each row from the Meals table (code below)
            List <ViewModels.MealListingVM> results = new List <ViewModels.MealListingVM>();

            // System.Data.SQLite.SQLiteDataReader dataReader =

            using (SQLiteConnection con = new SQLiteConnection(conString))
            {
                con.Open();

                string query = "SELECT MealID, MealName, MealIngredients FROM Meals";

                using (SQLiteCommand cmd = new SQLiteCommand(query, con))
                {
                    using (SQLiteDataReader dataReader = cmd.ExecuteReader())
                    {
                        if (dataReader.HasRows)
                        {
                            while (dataReader.Read())
                            {
                                ViewModels.MealListingVM meal = new ViewModels.MealListingVM();

                                meal.MealID          = Convert.ToInt32(dataReader["MealID"]);
                                meal.MealName        = Convert.ToString(dataReader["MealName"]);
                                meal.MealIngredients = Convert.ToString(dataReader["MealIngredients"]);
                                // Add meals to result list which we send to Controller
                                results.Add(meal);
                            }
                        }
                    }
                }
            }
            return(results);
        }