210 lines
6.2 KiB
C#
210 lines
6.2 KiB
C#
using Microsoft.Data.SqlClient;
|
|
using MLL;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
|
|
namespace DALL
|
|
{
|
|
public class DALBancos
|
|
{
|
|
private readonly string _conexao;
|
|
|
|
public DALBancos(string conexao)
|
|
{
|
|
_conexao = conexao;
|
|
}
|
|
|
|
#region INSERT
|
|
public bool Inserir(string numero, string nome)
|
|
{
|
|
try
|
|
{
|
|
using (SqlConnection conn = new SqlConnection(_conexao))
|
|
{
|
|
string sql = @"INSERT INTO Bancos (NUMERO, NOME)
|
|
VALUES (@NUMERO, @NOME)";
|
|
|
|
using (SqlCommand cmd = new SqlCommand(sql, conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@NUMERO", (object?)numero ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@NOME", (object?)nome ?? DBNull.Value);
|
|
|
|
conn.Open();
|
|
return cmd.ExecuteNonQuery() > 0;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}//Inserir
|
|
#endregion
|
|
|
|
#region UPDATE
|
|
public bool Alterar(int id, string numero, string nome)
|
|
{
|
|
try
|
|
{
|
|
using (SqlConnection conn = new SqlConnection(_conexao))
|
|
{
|
|
string sql = @"UPDATE Bancos
|
|
SET NUMERO = @NUMERO,
|
|
NOME = @NOME
|
|
WHERE ID_BANCOS = @ID";
|
|
|
|
using (SqlCommand cmd = new SqlCommand(sql, conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@ID", id);
|
|
cmd.Parameters.AddWithValue("@NUMERO", (object?)numero ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@NOME", (object?)nome ?? DBNull.Value);
|
|
|
|
conn.Open();
|
|
return cmd.ExecuteNonQuery() > 0;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}//alterar
|
|
#endregion
|
|
|
|
#region DELETE
|
|
public bool Excluir(int id)
|
|
{
|
|
try
|
|
{
|
|
using (SqlConnection conn = new SqlConnection(_conexao))
|
|
{
|
|
string sql = "DELETE FROM Bancos WHERE ID_BANCOS = @ID";
|
|
|
|
using (SqlCommand cmd = new SqlCommand(sql, conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@ID", id);
|
|
|
|
conn.Open();
|
|
return cmd.ExecuteNonQuery() > 0;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}//Excluir
|
|
#endregion
|
|
|
|
#region SELECT TODOS (DataTable)
|
|
public DataTable LocalizarTodos()
|
|
{
|
|
DataTable tabela = new DataTable();
|
|
|
|
using (SqlConnection conn = new SqlConnection(_conexao))
|
|
{
|
|
string sql = "SELECT * FROM Bancos ORDER BY NOME";
|
|
|
|
using (SqlDataAdapter da = new SqlDataAdapter(sql, conn))
|
|
{
|
|
da.Fill(tabela);
|
|
}
|
|
}
|
|
|
|
return tabela;
|
|
}//LocalizarTodos
|
|
#endregion
|
|
|
|
#region SELECT LISTA (opcional - forte tipagem)
|
|
public List<(int id, string numero, string nome)> ListarLista()
|
|
{
|
|
var lista = new List<(int, string, string)>();
|
|
|
|
using (SqlConnection conn = new SqlConnection(_conexao))
|
|
{
|
|
string sql = "SELECT ID_BANCOS, NUMERO, NOME FROM Bancos ORDER BY NOME";
|
|
|
|
using (SqlCommand cmd = new SqlCommand(sql, conn))
|
|
{
|
|
conn.Open();
|
|
|
|
using (SqlDataReader dr = cmd.ExecuteReader())
|
|
{
|
|
while (dr.Read())
|
|
{
|
|
lista.Add((
|
|
Convert.ToInt32(dr["ID_BANCOS"]),
|
|
dr["NUMERO"]?.ToString(),
|
|
dr["NOME"]?.ToString()
|
|
));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return lista;
|
|
}//ListarLista
|
|
#endregion
|
|
|
|
#region SELECT POR NUMERO
|
|
public string ObterNomePorNumero(string numero)
|
|
{
|
|
try
|
|
{
|
|
using (SqlConnection conn = new SqlConnection(_conexao))
|
|
{
|
|
string sql = "SELECT NOME FROM Bancos WHERE NUMERO = @NUMERO";
|
|
|
|
using (SqlCommand cmd = new SqlCommand(sql, conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@NUMERO", numero);
|
|
|
|
conn.Open();
|
|
|
|
object result = cmd.ExecuteScalar();
|
|
|
|
return result != null ? result.ToString() : null;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}//ObterNomePorNumero
|
|
#endregion
|
|
public ModeloBancos CarregarModeloBanco(int cod)
|
|
{
|
|
ModeloBancos banco = null;
|
|
|
|
using (SqlConnection conn = new SqlConnection(_conexao))
|
|
{
|
|
string sql = "SELECT * FROM Bancos WHERE ID_BANCOS = @ID";
|
|
|
|
using (SqlCommand cmd = new SqlCommand(sql, conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@ID", cod);
|
|
|
|
conn.Open();
|
|
|
|
using (SqlDataReader dr = cmd.ExecuteReader())
|
|
{
|
|
if (dr.Read())
|
|
{
|
|
banco = new ModeloBancos
|
|
{
|
|
ID_BANCOS = Convert.ToInt32(dr["ID_BANCOS"]),
|
|
NUMERO = dr["NUMERO"]?.ToString(),
|
|
NOME = dr["NOME"]?.ToString()
|
|
};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return banco;
|
|
}//CarregarModeloBanco
|
|
|
|
}
|
|
} |