public void Salvar(EntidadeDominio entidade) { Endereco endereco = (Endereco)entidade; #region Conexão BD Conexao conn = new Conexao(); var conexao = conn.Connection(); var objConn = new SqlConnection(conexao); if (objConn.State == ConnectionState.Closed) { objConn.Open(); } var objComando = new SqlCommand(); objComando.Connection = objConn; #endregion try { TipoDAO tipoDao = new TipoDAO(); tipoDao.Salvar(endereco.GetTpEndereco()); ClienteDAO clienteDao = new ClienteDAO(); StringBuilder strSQL = new StringBuilder(); strSQL.Append("INSERT INTO tb_endereco(cli_id, tpend_id, cidade, estado,"); strSQL.Append("logradouro, numero, cep) VALUES (@cli_id, @tpend_id, @cidade, @estado, @logradouro, @numero, @cep)"); objComando.CommandText = strSQL.ToString(); objComando.Parameters.AddWithValue("@cli_id", clienteDao.ConsultarId()); objComando.Parameters.AddWithValue("@tpend_id", tipoDao.ConsultarId(endereco.GetTpEndereco())); objComando.Parameters.AddWithValue("@cidade", endereco.GetCidade().GetDescricao()); objComando.Parameters.AddWithValue("@estado", endereco.GetCidade().GetEstado().getDescricao()); objComando.Parameters.AddWithValue("@logradouro", endereco.GetLogradouro()); objComando.Parameters.AddWithValue("@numero", endereco.GetNumero()); objComando.Parameters.AddWithValue("@cep", endereco.GetCep()); if (objComando.ExecuteNonQuery() < 1) { throw new Exception("Erro ao inserir registro"); } objConn.Close(); } catch (Exception ex) { if (objConn.State == ConnectionState.Open) { objConn.Close(); } throw new Exception("Erro ao inserir registro " + ex.Message); } }
public void Alterar(EntidadeDominio entidade) { Endereco endereco = (Endereco)entidade; #region Conexão BD Conexao conn = new Conexao(); var conexao = conn.Connection(); var objConn = new SqlConnection(conexao); if (objConn.State == ConnectionState.Closed) { objConn.Open(); } var objComando = new SqlCommand(); objComando.Connection = objConn; #endregion try { TipoDAO tipoDao = new TipoDAO(); tipoDao.Alterar(endereco.GetTpEndereco()); ClienteDAO clienteDao = new ClienteDAO(); StringBuilder strSQL = new StringBuilder(); strSQL.Append("UPDATE tb_endereco SET "); strSQL.Append("cidade = @cidade, estado = @estado, logradouro = @logradouro, numero = @numero, cep = @cep "); strSQL.Append("WHERE id = @id"); objComando.CommandText = strSQL.ToString(); objComando.Parameters.AddWithValue("@cidade", endereco.GetCidade().GetDescricao()); objComando.Parameters.AddWithValue("@estado", endereco.GetCidade().GetEstado().getDescricao()); objComando.Parameters.AddWithValue("@logradouro", endereco.GetLogradouro()); objComando.Parameters.AddWithValue("@numero", endereco.GetNumero()); objComando.Parameters.AddWithValue("@cep", endereco.GetCep()); if (objComando.ExecuteNonQuery() < 1) { throw new Exception("Erro ao inserir registro"); } objConn.Close(); } catch (Exception ex) { if (objConn.State == ConnectionState.Open) { objConn.Close(); } throw new Exception("Erro ao inserir registro " + ex.Message); } }
internal int Gravar(Endereco Endereco) { int Quantidade = new FilialDAO().ValidarEnderecoFilial(Endereco.GetCodigo()); int Codigo; if (Quantidade > 1) { b.getComandoSQL().Parameters.Clear(); b.getComandoSQL().CommandText = @"insert into Endereco (end_referencia, end_logradouro, end_numero, end_bairro, end_cep, end_cidade, end_estado, end_stativo) values( @referencia, @logradouro, @numero, @bairro, @cep, @cidade, @estado, 1); SELECT LAST_INSERT_ID();"; b.getComandoSQL().Parameters.AddWithValue("@referencia", Endereco.GetReferencia()); b.getComandoSQL().Parameters.AddWithValue("@logradouro", Endereco.GetLogradouro()); b.getComandoSQL().Parameters.AddWithValue("@numero", Endereco.GetNumero()); b.getComandoSQL().Parameters.AddWithValue("@bairro", Endereco.GetBairro()); b.getComandoSQL().Parameters.AddWithValue("@cep", Endereco.GetCep()); b.getComandoSQL().Parameters.AddWithValue("@cidade", Endereco.GetCidade()); b.getComandoSQL().Parameters.AddWithValue("@estado", Endereco.GetEstado()); if (b.ExecutaComando(true, out Codigo) == 1) { return(Codigo); } else { return(-10); } } else { b.getComandoSQL().Parameters.Clear(); if (Endereco.GetCodigo() == 0) { b.getComandoSQL().CommandText = @"insert into Endereco (end_referencia, end_logradouro, end_numero, end_bairro, end_cep, end_cidade, end_estado, end_stativo) values( @referencia, @logradouro, @numero, @bairro, @cep, @cidade, @estado, 1); SELECT LAST_INSERT_ID();"; } else { b.getComandoSQL().CommandText = @"update Endereco set end_referencia = @referencia, end_logradouro = @logradouro, end_numero = @numero, end_bairro = @bairro, end_cep = @cep, end_cidade = @cidade, end_estado = @estado where end_codigo = @codigo;"; b.getComandoSQL().Parameters.AddWithValue("@codigo", Endereco.GetCodigo()); } b.getComandoSQL().Parameters.AddWithValue("@referencia", Endereco.GetReferencia()); b.getComandoSQL().Parameters.AddWithValue("@logradouro", Endereco.GetLogradouro()); b.getComandoSQL().Parameters.AddWithValue("@numero", Endereco.GetNumero()); b.getComandoSQL().Parameters.AddWithValue("@bairro", Endereco.GetBairro()); b.getComandoSQL().Parameters.AddWithValue("@cep", Endereco.GetCep()); b.getComandoSQL().Parameters.AddWithValue("@cidade", Endereco.GetCidade()); b.getComandoSQL().Parameters.AddWithValue("@estado", Endereco.GetEstado()); if (Endereco.GetCodigo() == 0) { if (b.ExecutaComando(true, out Codigo) == 1) { return(Codigo); } else { return(-10); } } else { if (b.ExecutaComando(true) == 1) { return(Endereco.GetCodigo()); } else { return(-10); } } } }