Exemplo n.º 1
0
 public Ingredient(int id, string name, int quantity, Unit unit)
 {
     this.Id = id;
     this.Name = name;
     this.Quantity = quantity;
     this.Unit = unit;
 }
Exemplo n.º 2
0
        public static Unit Create(string name, string abbreviation)
        {
            Unit unit;
            // Insert new unit
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString))
            {
                using (SqlCommand command = new SqlCommand("dbo.Unit_Insert", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    //command.Parameters.AddWithValue("@UnitID", 0).Direction = ParameterDirection.Output;
                    command.Parameters.Add("@UnitID", SqlDbType.Int);
                    command.Parameters["@UnitID"].Direction = ParameterDirection.Output;
                    command.Parameters.AddWithValue("@UnitName", name);
                    command.Parameters.AddWithValue("@UnitAbbreviation", abbreviation);

                    connection.Open();
                    command.ExecuteNonQuery();
                    unit = new Unit(Convert.ToInt32(command.Parameters["@UnitID"].Value), name, abbreviation);
                    connection.Close();
                } // SqlCommand
            } // SqlConnection
            return unit;
        }
Exemplo n.º 3
0
        public static Ingredient Create(string name, int quantity, Unit unit)
        {
            Ingredient ingredient;
            // Insert new ingredient
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString))
            {
                using (SqlCommand command = new SqlCommand("dbo.Ingredient_Insert", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    //command.Parameters.AddWithValue("@IngredientID", 0).Direction = ParameterDirection.Output;
                    command.Parameters.Add("@IngredientID", SqlDbType.Int);
                    command.Parameters["@IngredientID"].Direction = ParameterDirection.Output;
                    command.Parameters.AddWithValue("@IngredientName", name);
                    command.Parameters.AddWithValue("@IngredientQuantity", quantity);
                    command.Parameters.AddWithValue("@UnitID", unit.Id);

                    connection.Open();
                    command.ExecuteNonQuery();
                    ingredient = new Ingredient((int)command.Parameters["@IngredientID"].Value, name, quantity, unit);
                    connection.Close();
                } // SqlCommand
            } // SqlConnection
            return ingredient;
        }
Exemplo n.º 4
0
        public static Unit GetUnitByID(int id)
        {
            Unit unit = null;
            // Select unit
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString))
            {
                using (SqlCommand command = new SqlCommand("db_datareader.Unit_Select", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@UnitID", id);
                    connection.Open();

                    using (SqlDataReader dataReader = command.ExecuteReader())
                    {
                        if (dataReader.HasRows && dataReader.Read())
                        {
                            unit = new Unit(id, (string)dataReader["UnitName"], (string)dataReader["UnitAbbreviation"]);
                        }
                    } // SqlDataReader
                } // SqlCommand
            } // SqlConnection
            return unit;
        }
Exemplo n.º 5
0
        public static List<Unit> GetUnits()
        {
            List<Unit> units = new List<Unit>();

            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString))
            {
                using (SqlCommand command = new SqlCommand("db_datareader.Units_Select", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    connection.Open();

                    using (SqlDataReader dataReader = command.ExecuteReader())
                    {
                        while (dataReader.Read())
                        {
                            Unit unit = new Unit();
                            unit.Id = Convert.ToInt32(dataReader["UnitID"]);
                            unit.Name = (string)dataReader["UnitName"];
                            unit.Abbreviation = (string)dataReader["UnitAbbreviation"];
                            units.Add(unit);
                        }
                    } // SqlDataReader
                } // SqlCommand
            } // SqlConnection

            return units;
        }