Esempio n. 1
0
        public OperationResult Create(Suplidor suplidor)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                SqlTransaction tran = conn.BeginTransaction();

                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "createSuplidor";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection  = conn;
                    cmd.Transaction = tran;
                    cmd.Parameters.AddWithValue("@Nombre", suplidor.Nombre);
                    cmd.Parameters.AddWithValue("@RNC", suplidor.RNC);
                    cmd.Parameters.AddWithValue("@Direccion", suplidor.Direccion);
                    cmd.Parameters.AddWithValue("@Representante", suplidor.Representante);

                    try
                    {
                        cmd.ExecuteNonQuery();
                        tran.Commit();
                        return(new OperationResult(true, "Suplidor creado satisfactoriamente!"));
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        return(new OperationResult(false, $"Ha ocurrido un error. {ex.Message}"));
                    }
                }
            }
        }
Esempio n. 2
0
        public OperationResult Update(Suplidor suplidor, string rnc)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                SqlTransaction tran = conn.BeginTransaction();

                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "updateSuplidor";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection  = conn;
                    cmd.Transaction = tran;
                    cmd.Parameters.AddWithValue("@Direccion", suplidor.Direccion);
                    cmd.Parameters.AddWithValue("@Representante", suplidor.Representante);
                    cmd.Parameters.AddWithValue("@RNC", suplidor.RNC);

                    try
                    {
                        var result = cmd.ExecuteNonQuery();
                        if (result == 0)
                        {
                            return(new OperationResult(false, $"No se encontró Suplidor con RNC {rnc}."));
                        }
                        tran.Commit();
                        return(new OperationResult()
                        {
                            Result = true, Message = "Suplidor actualizado con éxito"
                        });
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        return(new OperationResult(false, $"Ha ocurrido un error. {ex.Message}"));
                    }
                }
            }
        }