Вставить данные в базу данных с помощью транзакции - PullRequest
0 голосов
/ 02 апреля 2020

Я пытаюсь вставить набор данных в базу данных. Когда я вставляю, вставляется только последняя запись. Я использую транзакции. Ниже мой код. Для обновления таблицы я использую функцию удаления и вставки. Если данные уже присутствуют, их необходимо удалить, а затем вставить.

 public bool SaveLocationPrices(IEnumerable<InventoryLocation> locationprices, int createdBy)
    {
        var sSQL = "";
        var sqSQL = "";
        using (var dbClient = new DatabaseClient(InstanceMgr.ProcessInstance))
        {
            try
            {
                dbClient.BeginSmartTransaction();
                foreach(var invLocationPrice in locationprices)
                {
                    string sguid = invLocationPrice.InvLocationRetailId.ToString();
                    string iMasterId = invLocationPrice.InvMasterID.ToString();
                    string sLocId = invLocationPrice.LocationId.ToString();
                    var bTaxable  = (invLocationPrice.Taxable == true) ? "Y": "N";
                    var bRound = (invLocationPrice.Round == true) ? "Y" : "N";

                    var parameters1 = new List<SmartParam>()
                    {
                         new SmartParam("@InvLocRetailId", sguid)
                    };
                    if (sguid.Length == 36)
                    {
                         sSQL = @"delete from retail_xref where invloc_retail_id = @InvLocRetailId";
                        dbClient.ExecuteNonQuery(sSQL, parameters1.ToArray());
                    }
                    else
                    {
                        Guid nguid = new Guid();
                        sguid = nguid.ToString();

                    }

                    if (invLocationPrice.RetailPrice>0)
                    {
                        sqSQL = @"INSERT INTO retail_xref (invloc_retail_id, invmst_id, Location_id, add_on, markup, mask, round_ind, retail_price,taxable_ind, reorder_level, created_by, create_timestamp, modified_by, modify_timestamp )" +
                                @"values (@InvLocRetailId,@InvMasterId,@LocationId,@AddOn,@MarkUp,@Mask,@Round,@RetailPrice,@Taxable,@ReorderLevel,@CreatedBy, {fn Now()},@CreatedBy, {fn Now()} )";
                        var parameters = new List<SmartParam>()
                    {
                         new SmartParam("@InvLocRetailId", sguid),
                         new SmartParam("@InvMasterId",iMasterId),
                         new SmartParam("@LocationId",sLocId),
                         new SmartParam("@AddOn",invLocationPrice.AddOn.ToString()),
                         new SmartParam("@MarkUp",invLocationPrice.Markup.ToString()),
                         new SmartParam("@Mask",invLocationPrice.Mask.ToString()),
                         new SmartParam("@Round",bRound.ToString()),
                         new SmartParam("@RetailPrice",invLocationPrice.RetailPrice.ToString()),
                         new SmartParam("@Taxable",bTaxable.ToString()),
                         new SmartParam("@ReorderLevel",invLocationPrice.ReorderLevel.ToString()),
                         new SmartParam("@CreatedBy",createdBy.ToString())

                    };

                        dbClient.ExecuteNonQuery(sqSQL, parameters.ToArray());
                    }

                }
                dbClient.CommitSmartTransaction();
            }
            catch (Exception ex)
            {
                dbClient.RollbackSmartTransaction();
                throw ex;
            }
        }
            return true;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...