I appreciate on first glance this could seem obvious but I'm not sure on the context. I'm making an application that calculates a shopping list from a given meal plan. This involves many iterations, tables and connections to the database. Tables include:
MealPlan (Contains the mealID for 6 meals a day for 7 days)
Meal (Contains the name of each meal and an ID)
Ingredients (Contains every ingredient on the system)
MealIngredients (Contains a foreign key of mealID and ingredientID, showing which ingredients and their quantities relate to a meal)
If each meal contains, on average, 10 ingredients and I've got 6 meals a day for 7 days of the week, that's 420 separate connections to the database to get the data for particular ingredients.
So, instead, would it be quicker/more efficient to select the entire table?
Option 1 (New connection each time):
ingredientID = 5
For Each row As DataRow In ingredients.table.Rows
ingredients.setTableContents("SELECT `quantity` , `name`, `measurement` FROM `tblIngredients` WHERE ingredientID=" + ingredientID)
'Do stuff with the ingredients attributes
Next
Option 2 (Select entire table and reference DataTable):
ingredientID = 5
dataTable = ingredients.setTableContents("SELECT `quantity` , `name`, `measurement` FROM `tblIngredients`")
For Each row As DataRow In ingredients.table.Rows
quantity = table.Rows(mealID).Item(0).ToString()
Next