public void Atualizar(RegraFrete p, List <CidadeRegraFrete> listaCidades) { try { AbrirConexao(); strSQL = "UPDATE [dbo].[REGRA_DE_FRETE] " + "SET[CD_TRANSPORTADOR] = @v1 " + ",[DE_PARA_11] = @v2 " + ",[DE_PARA_12] = @v3 " + ",[DE_PARA_21] = @v4 " + ",[DE_PARA_22] = @v5 " + ",[DE_PARA_PCT_11] = @v6 " + ",[DE_PARA_PCT_12] = @v7 " + ",[VL_FRETE_MINIMO] = @v8 " + ",[VL_GRIS] = @v9 " + ",[DE_PARA_31] = @v10 " + ",[DE_PARA_32] = @v11 " + ",[REGIAO] = @v12 " + "WHERE [CD_INDEX] = @CODIGO"; Cmd = new SqlCommand(strSQL, Con); Cmd.Parameters.AddWithValue("@CODIGO", p.CodigoIndex); Cmd.Parameters.AddWithValue("@v1", p.CodigoTransportador); Cmd.Parameters.AddWithValue("@v2", p.DePara11); Cmd.Parameters.AddWithValue("@v3", p.DePara12); Cmd.Parameters.AddWithValue("@v4", p.DePara21); Cmd.Parameters.AddWithValue("@v5", p.DePara22); Cmd.Parameters.AddWithValue("@v6", p.DeParaPct11); Cmd.Parameters.AddWithValue("@v7", p.DeParaPct12); Cmd.Parameters.AddWithValue("@v8", p.ValorFreteMinimo); Cmd.Parameters.AddWithValue("@v9", p.ValorGRIS); Cmd.Parameters.AddWithValue("@v10", p.DePara31); Cmd.Parameters.AddWithValue("@v11", p.DePara32); Cmd.Parameters.AddWithValue("@v12", p.Regiao); Cmd.ExecuteNonQuery(); } catch (Exception ex) { throw new Exception("Erro ao atualizar REGRA DE FRETE: " + ex.Message.ToString()); } finally { FecharConexao(); CidadeRegraFreteDAL citDAL = new CidadeRegraFreteDAL(); citDAL.Inserir(listaCidades, p.CodigoIndex); } }
public void Inserir(RegraFrete p, List <CidadeRegraFrete> listaCidades) { try { AbrirConexao(); strSQL = "INSERT INTO [dbo].[REGRA_DE_FRETE] " + "([CD_TRANSPORTADOR] " + ",[DE_PARA_11] " + ",[DE_PARA_12] " + ",[DE_PARA_21] " + ",[DE_PARA_22] " + ",[DE_PARA_PCT_11] " + ",[DE_PARA_PCT_12] " + ",[VL_FRETE_MINIMO] " + ",[VL_GRIS] " + ",[DE_PARA_31] " + ",[DE_PARA_32] " + ",[REGIAO]) " + "values(@v1,@v2,@v3,@v4,@v5,@v6,@v7,@v8,@v9,@v10,@v11,@v12) SELECT SCOPE_IDENTITY();"; Cmd = new SqlCommand(strSQL, Con); Cmd.Parameters.AddWithValue("@v1", p.CodigoTransportador); Cmd.Parameters.AddWithValue("@v2", p.DePara11); Cmd.Parameters.AddWithValue("@v3", p.DePara12); Cmd.Parameters.AddWithValue("@v4", p.DePara21); Cmd.Parameters.AddWithValue("@v5", p.DePara22); Cmd.Parameters.AddWithValue("@v6", p.DeParaPct11); Cmd.Parameters.AddWithValue("@v7", p.DeParaPct12); Cmd.Parameters.AddWithValue("@v8", p.ValorFreteMinimo); Cmd.Parameters.AddWithValue("@v9", p.ValorGRIS); Cmd.Parameters.AddWithValue("@v10", p.DePara31); Cmd.Parameters.AddWithValue("@v11", p.DePara32); Cmd.Parameters.AddWithValue("@v12", p.Regiao); p.CodigoIndex = Convert.ToInt32(Cmd.ExecuteScalar()); } catch (SqlException ex) { if (ex.Errors.Count > 0) // Assume the interesting stuff is in the first error { switch (ex.Errors[0].Number) { case 2601: // Primary key violation throw new DuplicateNameException("Inclusão não Permitida!!! Chave já consta no Banco de Dados. Mensagem :" + ex.Message.ToString(), ex); case 2627: // Primary key violation throw new DuplicateNameException("Inclusão não Permitida!!! Chave já consta no Banco de Dados. Mensagem :" + ex.Message.ToString(), ex); default: throw new Exception("Erro ao Incluir REGRA DE FRETE: " + ex.Message.ToString()); } } } catch (Exception ex) { throw new Exception("Erro ao gravar REGRA DE FRETE: " + ex.Message.ToString()); } finally { FecharConexao(); CidadeRegraFreteDAL citDAL = new CidadeRegraFreteDAL(); citDAL.Inserir(listaCidades, p.CodigoIndex); } }