public void Atualizar(Proposta proposta) { try { this.Abrir(); string sql = "UPDATE proposta SET titulo = @titulo WHERE proposta_id = @proposta_id"; SqlCommand cmd = new SqlCommand(sql, this.sqlConn); cmd.Parameters.Add("@titulo", SqlDbType.VarChar); cmd.Parameters["@titulo"].Value = proposta.Titulo; cmd.Parameters.Add("@proposta_id", SqlDbType.Int); cmd.Parameters["@proposta_id"].Value = proposta.Proposta_id; DadosItem di = new DadosItem(); foreach (Item i in proposta.Items) { i.Proposta = new Proposta(); i.Proposta.Proposta_id = proposta.Proposta_id; di.Cadastrar(i); } cmd.ExecuteNonQuery(); cmd.Dispose(); this.Fechar(); } catch (Exception ex) { throw new Exception("Erro ao atualizar proposta: " + ex.Message); } }
public void Cadastrar(Proposta proposta) { try { this.Abrir(); string sql = "INSERT INTO proposta (orcamento_id, titulo) OUTPUT INSERTED.proposta_id VALUES (@orcamento_id, @titulo) "; SqlCommand cmd = new SqlCommand(sql, this.sqlConn); cmd.Parameters.Add("@orcamento_id", SqlDbType.Int); cmd.Parameters["@orcamento_id"].Value = proposta.Orcamento.Orcamento_id; cmd.Parameters.Add("@titulo", SqlDbType.VarChar); cmd.Parameters["@titulo"].Value = proposta.Titulo; //cmd.ExecuteNonQuery(); int proposta_id = Convert.ToInt32(cmd.ExecuteScalar()); DadosItem di = new DadosItem(); foreach (Item i in proposta.Items) { i.Proposta = new Proposta(); i.Proposta.Proposta_id = proposta_id; di.Cadastrar(i); } cmd.Dispose(); this.Fechar(); } catch (Exception ex) { throw new Exception("Erro ao cadastrar proposta: " + ex.Message); } }