示例#1
0
        private void bAdd_Click(object sender, EventArgs e)
        {
            Ingredient ingredient = new Ingredient();

            ingredient.RecipeId = mRecipeId;
            ingredient.Name = tbName.Text;
            ingredient.Amount = tbAmount.Text;
            ingredient.Measure = tbMeasure.Text;

            bool successfull = true;
            try
            {
                successfull = mDatabaseConnection.ExecuteNonReturnQuery(ingredient.GetInsertQuery());
            }
            catch (SqlException ex)
            {
                successfull = false;
                MessageBox.Show(ex.Message, "Error:" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (successfull)
            {
                MessageBox.Show("Het ingredient is toegevoegd.", "Toevoegen gelukt.");
                mForm.ReloadRecipes();
                this.Close();
            }
        }
        private List<Ingredient> GetAllIngredients(int recipeId)
        {
            string query = "SELECT * FROM ingredients WHERE recipeId = @P0 ORDER BY name";
            //SqlCommand mSqlCommand = new SqlCommand(query, mSqlConnection);
            //mSqlCommand.Parameters.AddWithValue("P0", recipeId);

            OleDbCommand command = new OleDbCommand(query, mConnection);
            command.Parameters.AddWithValue("P0", recipeId);

            //SqlDataReader mSqlDataReader = null;
            OleDbDataReader dataReader = null;

            List<Ingredient> ingredientList = new List<Ingredient>();
            try
            {
                //mSqlConnection.Open();
                //mSqlDataReader = mSqlCommand.ExecuteReader();

                mConnection.Open();
                dataReader = command.ExecuteReader();

                // Check is the reader has any rows at all before starting to read.
                //if (mSqlDataReader.HasRows)
                if (dataReader.HasRows)
                {
                    // Read advances to the next row.
                    //while (mSqlDataReader.Read())
                    while (dataReader.Read())
                    {
                        Ingredient ingredient = new Ingredient();

                        ingredient.Id = dataReader.GetInt32(dataReader.GetOrdinal("id"));
                        ingredient.RecipeId = dataReader.GetInt32(dataReader.GetOrdinal("recipeId"));
                        ingredient.Name = dataReader.GetString(dataReader.GetOrdinal("name"));
                        ingredient.Amount = dataReader.GetString(dataReader.GetOrdinal("amount"));
                        ingredient.Measure = dataReader.GetString(dataReader.GetOrdinal("measure"));

                        ingredientList.Add(ingredient);
                    }
                }
            }
            //catch (SqlException ex)
            catch (OleDbException ex)
            {
                MessageBox.Show(ex.Message, "Error:" + ex.ErrorCode, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                //mSqlDataReader.Close();
                //mSqlConnection.Close();

                dataReader.Close();
                mConnection.Close();
            }

            return ingredientList;
        }