22/06/2026 - DAL Itens (50% concluida).
This commit is contained in:
parent
c7db527039
commit
aa04fdf054
286
DAL/DALIcmsUf.cs
Normal file
286
DAL/DALIcmsUf.cs
Normal file
@ -0,0 +1,286 @@
|
|||||||
|
using Microsoft.Data.SqlClient;
|
||||||
|
using MLL;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
|
||||||
|
namespace DALL
|
||||||
|
{
|
||||||
|
public class DALIcmsUf
|
||||||
|
{
|
||||||
|
private readonly string connectionString;
|
||||||
|
|
||||||
|
public DALIcmsUf(string conexao)
|
||||||
|
{
|
||||||
|
connectionString = conexao;
|
||||||
|
}
|
||||||
|
|
||||||
|
#region INSERT
|
||||||
|
|
||||||
|
public bool Inserir(ModeloIcmsUf modelo)
|
||||||
|
{
|
||||||
|
using SqlConnection conn = new(connectionString);
|
||||||
|
|
||||||
|
string sql = @"
|
||||||
|
INSERT INTO ICMS_UF
|
||||||
|
(
|
||||||
|
COD_ITEM,
|
||||||
|
UF,
|
||||||
|
ICMS
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
(
|
||||||
|
@COD_ITEM,
|
||||||
|
@UF,
|
||||||
|
@ICMS
|
||||||
|
)";
|
||||||
|
|
||||||
|
using SqlCommand cmd = new(sql, conn);
|
||||||
|
|
||||||
|
PreencherParametros(cmd, modelo);
|
||||||
|
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
return cmd.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region UPDATE
|
||||||
|
|
||||||
|
public bool Alterar(ModeloIcmsUf modelo)
|
||||||
|
{
|
||||||
|
using SqlConnection conn = new(connectionString);
|
||||||
|
|
||||||
|
string sql = @"
|
||||||
|
UPDATE ICMS_UF SET
|
||||||
|
COD_ITEM=@COD_ITEM,
|
||||||
|
UF=@UF,
|
||||||
|
ICMS=@ICMS
|
||||||
|
WHERE ID_ICMS_UF=@ID_ICMS_UF";
|
||||||
|
|
||||||
|
using SqlCommand cmd = new(sql, conn);
|
||||||
|
|
||||||
|
PreencherParametros(cmd, modelo);
|
||||||
|
|
||||||
|
cmd.Parameters.AddWithValue(
|
||||||
|
"@ID_ICMS_UF",
|
||||||
|
modelo.ID_ICMS_UF);
|
||||||
|
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
return cmd.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region DELETE
|
||||||
|
|
||||||
|
public bool Excluir(int id)
|
||||||
|
{
|
||||||
|
using SqlConnection conn = new(connectionString);
|
||||||
|
|
||||||
|
using SqlCommand cmd = new(
|
||||||
|
"DELETE FROM ICMS_UF WHERE ID_ICMS_UF=@ID",
|
||||||
|
conn);
|
||||||
|
|
||||||
|
cmd.Parameters.AddWithValue("@ID", id);
|
||||||
|
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
return cmd.ExecuteNonQuery() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region CARREGAR
|
||||||
|
|
||||||
|
public ModeloIcmsUf? Carregar(int id)
|
||||||
|
{
|
||||||
|
using SqlConnection conn = new(connectionString);
|
||||||
|
|
||||||
|
using SqlCommand cmd = new(
|
||||||
|
"SELECT * FROM ICMS_UF WHERE ID_ICMS_UF=@ID",
|
||||||
|
conn);
|
||||||
|
|
||||||
|
cmd.Parameters.AddWithValue("@ID", id);
|
||||||
|
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
using SqlDataReader dr = cmd.ExecuteReader();
|
||||||
|
|
||||||
|
if (dr.Read())
|
||||||
|
return PreencherModelo(dr);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region CARREGAR POR CÓDIGO
|
||||||
|
|
||||||
|
public ModeloIcmsUf? CarregarPorCodigo(string codigo)
|
||||||
|
{
|
||||||
|
using SqlConnection conn = new(connectionString);
|
||||||
|
|
||||||
|
using SqlCommand cmd = new(
|
||||||
|
"SELECT * FROM ICMS_UF WHERE CODIGO=@CODIGO",
|
||||||
|
conn);
|
||||||
|
|
||||||
|
cmd.Parameters.AddWithValue("@CODIGO", codigo);
|
||||||
|
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
using SqlDataReader dr = cmd.ExecuteReader();
|
||||||
|
|
||||||
|
if (dr.Read())
|
||||||
|
return PreencherModelo(dr);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region LISTAR
|
||||||
|
|
||||||
|
public List<ModeloIcmsUf> Listar(string? codigo = null,string? codItem = null,string? uf = null)
|
||||||
|
{
|
||||||
|
List<ModeloIcmsUf> lista = [];
|
||||||
|
|
||||||
|
using SqlConnection conn = new(connectionString);
|
||||||
|
|
||||||
|
string sql = @"
|
||||||
|
SELECT *
|
||||||
|
FROM ICMS_UF
|
||||||
|
WHERE 1=1";
|
||||||
|
|
||||||
|
using SqlCommand cmd = new();
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(codigo))
|
||||||
|
{
|
||||||
|
sql += " AND CODIGO LIKE @CODIGO";
|
||||||
|
|
||||||
|
cmd.Parameters.AddWithValue(
|
||||||
|
"@CODIGO",
|
||||||
|
"%" + codigo + "%");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(codItem))
|
||||||
|
{
|
||||||
|
sql += " AND COD_ITEM=@COD_ITEM";
|
||||||
|
|
||||||
|
cmd.Parameters.AddWithValue(
|
||||||
|
"@COD_ITEM",
|
||||||
|
codItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(uf))
|
||||||
|
{
|
||||||
|
sql += " AND UF=@UF";
|
||||||
|
|
||||||
|
cmd.Parameters.AddWithValue(
|
||||||
|
"@UF",
|
||||||
|
uf);
|
||||||
|
}
|
||||||
|
|
||||||
|
sql += " ORDER BY UF";
|
||||||
|
|
||||||
|
cmd.Connection = conn;
|
||||||
|
cmd.CommandText = sql;
|
||||||
|
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
using SqlDataReader dr = cmd.ExecuteReader();
|
||||||
|
|
||||||
|
while (dr.Read())
|
||||||
|
{
|
||||||
|
lista.Add(
|
||||||
|
PreencherModelo(dr));
|
||||||
|
}
|
||||||
|
|
||||||
|
return lista;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region EXISTE
|
||||||
|
|
||||||
|
public bool Existe(string codItem, string uf)
|
||||||
|
{
|
||||||
|
using SqlConnection conn = new(connectionString);
|
||||||
|
|
||||||
|
string sql = @"
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM ICMS_UF
|
||||||
|
WHERE COD_ITEM=@COD_ITEM
|
||||||
|
AND UF=@UF";
|
||||||
|
|
||||||
|
using SqlCommand cmd = new(sql, conn);
|
||||||
|
|
||||||
|
cmd.Parameters.AddWithValue("@COD_ITEM", codItem);
|
||||||
|
cmd.Parameters.AddWithValue("@UF", uf);
|
||||||
|
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
return Convert.ToInt32(
|
||||||
|
cmd.ExecuteScalar()) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region QUANTIDADE TOTAL
|
||||||
|
|
||||||
|
public int QuantidadeTotal()
|
||||||
|
{
|
||||||
|
using SqlConnection conn = new(connectionString);
|
||||||
|
|
||||||
|
using SqlCommand cmd = new(
|
||||||
|
"SELECT COUNT(*) FROM ICMS_UF",
|
||||||
|
conn);
|
||||||
|
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
return Convert.ToInt32(
|
||||||
|
cmd.ExecuteScalar());
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region PREENCHER MODELO
|
||||||
|
|
||||||
|
private ModeloIcmsUf PreencherModelo(SqlDataReader dr)
|
||||||
|
{
|
||||||
|
return new ModeloIcmsUf
|
||||||
|
{
|
||||||
|
ID_ICMS_UF = Convert.ToInt32(dr["ID_ICMS_UF"]),
|
||||||
|
CODIGO = Convert.ToString(dr["CODIGO"]) ?? "",
|
||||||
|
COD_ITEM = Convert.ToString(dr["COD_ITEM"]) ?? "",
|
||||||
|
UF = Convert.ToString(dr["UF"]) ?? "",
|
||||||
|
ICMS = Convert.ToString(dr["ICMS"]) ?? ""
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region PREENCHER PARÂMETROS
|
||||||
|
|
||||||
|
private void PreencherParametros(
|
||||||
|
SqlCommand cmd,
|
||||||
|
ModeloIcmsUf modelo)
|
||||||
|
{
|
||||||
|
cmd.Parameters.AddWithValue(
|
||||||
|
"@COD_ITEM",
|
||||||
|
modelo.COD_ITEM);
|
||||||
|
|
||||||
|
cmd.Parameters.AddWithValue(
|
||||||
|
"@UF",
|
||||||
|
modelo.UF);
|
||||||
|
|
||||||
|
cmd.Parameters.AddWithValue(
|
||||||
|
"@ICMS",
|
||||||
|
modelo.ICMS);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
1508
DAL/DALItens.cs
Normal file
1508
DAL/DALItens.cs
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user