Exemplo n.º 1
0
 /// <summary>
 ///
 /// </summary>
 public void BeginTransaction()
 {
     if (transaction == null)
     {
         initialTransactionHolder = true;
         transaction = connection.BeginTransaction();
         DbContext.Current.EnterTransactionalState(transaction);
     }
     else
     {
         string savePointName = "tr_" + (Guid.NewGuid().ToString().Replace("-", ""));
         transaction.Save(savePointName);
         transactionStack.Push(savePointName);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// トランザクション設定
        /// </summary>
        public void BeginTransaction()
        {
            if (isTran && isSetSavePoint)
            {
                throw new Exception("トランザクションが設定できませんでした。");
            }

            if (isTran)
            {
                tran.Save(SavePointName);
                isSetSavePoint = true;
            }
            else
            {
                this.tran   = this.conn.BeginTransaction();
                this.isTran = true;
            }
        }
 public void SavePoint(string savePoint)
 {
     _transaction?.Save(savePoint);
 }
Exemplo n.º 4
0
        public override int cambiarEstado(string id, bool anulado)
        {
            anulado = true;
            bool anuloVenta    = false;
            bool anuloDetalle  = false;
            bool devolvioStock = false;

            this.filasAfectadas = 0;

            if (conexionPostgreSql.abrirConexion())
            {
                NpgsqlTransaction   anularVentaTransaction       = null;
                List <DetalleVenta> detallesQueContieneProductos = new List <DetalleVenta>();

                try
                {
                    anularVentaTransaction = conexionPostgreSql.retornarConexion().BeginTransaction();
                    anularVentaTransaction.Save("antesDeAnularVenta");

                    try
                    {
                        string        consultaVenta = "UPDATE venta set anulada = @Anulada WHERE idventa = @IdVenta";
                        NpgsqlCommand commandVenta  = new NpgsqlCommand(consultaVenta, conexionPostgreSql.retornarConexion(), anularVentaTransaction);
                        commandVenta.Parameters.Add("@Anulada", NpgsqlTypes.NpgsqlDbType.Boolean);
                        commandVenta.Parameters.Add("@IdVenta", NpgsqlTypes.NpgsqlDbType.Integer);
                        commandVenta.Parameters[0].Value = anulado;
                        commandVenta.Parameters[1].Value = Convert.ToInt32(id);
                        if (commandVenta.ExecuteNonQuery() == 1)
                        {
                            anuloVenta = true;
                        }
                    }
                    catch (NpgsqlException ex)
                    {
                        anuloVenta = false;
                        MessageBox.Show(ex.ToString());
                        anularVentaTransaction.Rollback("antesDeAnularVenta");
                    }

                    try
                    {
                        string        consultaDetalle = "UPDATE detalleventa set anulado = @Anulado WHERE idventa = @IdVenta";
                        NpgsqlCommand commandDetalle  = new NpgsqlCommand(consultaDetalle, conexionPostgreSql.retornarConexion());
                        commandDetalle.Parameters.Add("@Anulado", NpgsqlTypes.NpgsqlDbType.Boolean);
                        commandDetalle.Parameters.Add("@IdVenta", NpgsqlTypes.NpgsqlDbType.Integer);
                        commandDetalle.Parameters[0].Value = anulado;
                        commandDetalle.Parameters[1].Value = Convert.ToInt32(id);
                        if (commandDetalle.ExecuteNonQuery() >= 1)
                        {
                            anuloDetalle = true;
                        }
                    }
                    catch (NpgsqlException ex)
                    {
                        anuloDetalle = false;
                        MessageBox.Show(ex.ToString());
                        anularVentaTransaction.Rollback("antesDeAnularVenta");
                    }

                    try
                    {
                        string        consultaDetallePorProductos = "SELECT idproducto, cantidad FROM detalleventa WHERE idventa = @IdVenta";
                        NpgsqlCommand commandBuscarDetalles       = new NpgsqlCommand(consultaDetallePorProductos, conexionPostgreSql.retornarConexion());
                        commandBuscarDetalles.Parameters.Add("@IdVenta", NpgsqlTypes.NpgsqlDbType.Integer);
                        commandBuscarDetalles.Parameters[0].Value = Convert.ToInt32(id);
                        NpgsqlDataReader dataReader = commandBuscarDetalles.ExecuteReader();
                        while (dataReader.Read())
                        {
                            detallesQueContieneProductos.Add(
                                new DetalleVenta
                            {
                                IdProducto = dataReader[0].ToString(),
                                Cantidad   = Convert.ToInt32(dataReader[1])
                            });
                        }
                    }
                    catch (NpgsqlException ex)
                    {
                        MessageBox.Show(ex.ToString());
                        anularVentaTransaction.Rollback("antesDeAnularVenta");
                    }


                    try
                    {
                        foreach (var item in detallesQueContieneProductos)
                        {
                            string        consultaProductos = "UPDATE producto SET stock = stock + " + item.Cantidad + " WHERE idproducto = '" + item.IdProducto + "'";
                            NpgsqlCommand commandProductos  = new NpgsqlCommand(consultaProductos, conexionPostgreSql.retornarConexion());
                            if (commandProductos.ExecuteNonQuery() >= 1)
                            {
                                devolvioStock = true;
                            }
                        }
                    }
                    catch (NpgsqlException ex)
                    {
                        devolvioStock = false;
                        MessageBox.Show(ex.ToString());
                        anularVentaTransaction.Rollback("antesDeAnularVenta");
                    }

                    anularVentaTransaction.Commit();
                }
                catch (NpgsqlException ex)
                {
                    MessageBox.Show(ex.ToString());
                    anularVentaTransaction.Rollback();
                }
                finally
                {
                    conexionPostgreSql.cerrarConexion();
                }
            }

            if (anuloVenta && anuloDetalle && devolvioStock)
            {
                this.filasAfectadas = 1;
            }

            return(this.filasAfectadas);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Creates a save point with the specified name.
 /// </summary>
 /// <param name="savePoint">The save point.</param>
 public void Save(string savePoint)
 {
     _currentTransaction.Save(savePoint);
 }