Пример #1
0
        static void TestaInclusao(string connectionString)
        {
            ClienteRepository cliRepo = new ClienteRepository(connectionString);
            ClienteModel      cli     = new ClienteModel()
            {
                Codigo   = 2,
                Nome     = "KELVIN CORREIA DO NASCIMENTO",
                Email    = "*****@*****.**",
                Telefone = "(11)99999-9999"
            };

            if (cliRepo.Inserir(cli))
            {
                Console.WriteLine("Sucesso na inclusão!");
            }
            else
            {
                Console.WriteLine("Erro !");
            }
        }
Пример #2
0
        public List <ClienteModel> Consultar()
        {
            var    clientes = new List <ClienteModel>();
            string sql      = "SELECT * FROM CLIENTES order by NOME";

            SqlConnection con = new SqlConnection(_connectionString);
            SqlCommand    cmd = new SqlCommand(sql, con);

            cmd.CommandType = CommandType.Text;
            SqlDataReader reader;

            con.Open();

            try
            {
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    var cli = new ClienteModel();
                    cli.Codigo   = Convert.ToInt32(reader[0].ToString());
                    cli.Nome     = reader[1].ToString();
                    cli.Email    = reader[2].ToString();
                    cli.Telefone = reader[3].ToString();

                    clientes.Add(cli);
                }
            }
            catch (Exception erro)
            {
                Console.WriteLine("Erro " + erro.ToString());
            }
            finally
            {
                con.Close();
            }

            return(clientes);
        }
Пример #3
0
        static void Inclusao(string connectionString)
        {
            ClienteModel cli = new ClienteModel()
            {
                Codigo   = 2,
                Nome     = "Rodrigo Henrique Paulino",
                Email    = "*****@*****.**",
                Telefone = "(11)99999-9999"
            };

            string comandoSql = $"INSERT INTO CLIENTES (codigo, nome, email, telefone ) values " +
                                $"( {cli.Codigo}, '{cli.Nome}', '{cli.Email}', '{cli.Telefone}' )";


            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand    cmd = new SqlCommand(comandoSql, con);

            cmd.CommandType = CommandType.Text;
            con.Open();
            try
            {
                var i = cmd.ExecuteNonQuery();
                if (i > 0)
                {
                    Console.WriteLine("Registro inserido com sucesso !");
                }
            }
            catch (Exception erro)
            {
                Console.WriteLine("Erro " + erro.ToString());
                throw;
            }
            finally
            {
                con.Close();
            }
        }
Пример #4
0
        static void Listagem(string connectionString)
        {
            string sql = "SELECT * FROM CLIENTES order by NOME";

            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand    cmd = new SqlCommand(sql, con);

            cmd.CommandType = CommandType.Text;
            SqlDataReader reader;

            con.Open();

            try
            {
                List <ClienteModel> clientes = new List <ClienteModel>();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    var cli = new ClienteModel();
                    cli.Codigo   = Convert.ToInt32(reader[0].ToString());
                    cli.Nome     = reader["nome"].ToString();
                    cli.Email    = reader["email"].ToString();
                    cli.Telefone = reader["telefone"].ToString();

                    clientes.Add(cli);
                }
            }
            catch (Exception erro)
            {
                Console.WriteLine("Erro " + erro.ToString());
            }
            finally
            {
                con.Close();
            }
        }
Пример #5
0
        public ClienteModel Consultar(int codigo)
        {
            ClienteModel cli = new ClienteModel();

            string sql = "SELECT * FROM CLIENTES where codigo=" + codigo;

            SqlConnection con = new SqlConnection(_connectionString);
            SqlCommand    cmd = new SqlCommand(sql, con);

            cmd.CommandType = CommandType.Text;
            SqlDataReader reader;

            con.Open();

            try
            {
                reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    cli.Codigo   = Convert.ToInt32(reader[0].ToString());
                    cli.Nome     = reader["nome"].ToString();
                    cli.Email    = reader["email"].ToString();
                    cli.Telefone = reader["telefone"].ToString();
                }
            }
            catch (Exception erro)
            {
                Console.WriteLine("Erro " + erro.ToString());
            }
            finally
            {
                con.Close();
            }

            return(cli);
        }