Пример #1
0
        public void UpdateMealRow(MealRow row)
        {
            using (var conn = CreateConnection())
                using (var tran = conn.BeginTransaction())
                {
                    try
                    {
                        conn.Execute(@"
UPDATE MealRow SET FoodId=@FoodId, PortionId=@PortionId, Quantity=@Quantity, Weight=@Weight WHERE Id=@Id;
DELETE FROM MealRowNutrient WHERE MealRowId=@Id;", row, tran);


                        var rowNutrientsTable = new[] { row }.NutrientsToDataTable();

                        using (var bulk = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, tran)
                        {
                            DestinationTableName = "MealRowNutrient"
                        })
                        {
                            bulk.WriteToServer(rowNutrientsTable);
                        }

                        tran.Commit();
                    }
                    catch
                    {
                        tran.Rollback();
                        throw;
                    }
                }
        }
Пример #2
0
 public void DeleteMealRow(MealRow row)
 {
     using (var conn = CreateConnection())
         using (var tran = conn.BeginTransaction())
         {
             try
             {
                 conn.Execute("DELETE FROM MealRow WHERE Id=@Id", row, tran);
                 tran.Commit();
             }
             catch
             {
                 tran.Rollback();
                 throw;
             }
         }
 }
Пример #3
0
        public void CreateMealRow(MealRow row, int index)
        {
            row.Id = Guid.NewGuid();
            using (var conn = CreateConnection())
                using (var tran = conn.BeginTransaction())
                {
                    try
                    {
                        conn.Execute("INSERT INTO MealRow(Id,MealId,[Index],FoodId,Quantity,PortionId,Weight) VALUES(@Id,@MealId,@Index,@FoodId,@Quantity,@PortionId,@Weight)", new
                        {
                            row.Id,
                            row.MealId,
                            Index = index,
                            row.FoodId,
                            row.Quantity,
                            row.PortionId,
                            row.Weight
                        }, tran);

                        var rowNutrientsTable = new[] { row }.NutrientsToDataTable();

                        using (var bulk = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, tran)
                        {
                            DestinationTableName = "MealRowNutrient"
                        })
                        {
                            bulk.WriteToServer(rowNutrientsTable);
                        }

                        tran.Commit();
                    }
                    catch
                    {
                        tran.Rollback();
                        throw;
                    }
                }
        }