Make select and move to the graphical interface

0

First the right model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MetaData
{
    public class Fornecedor
    {

        public int Codigo { get; set; }
        public string Nome { get; set; }
        public string Cnpj { get; set; }
        public string Telefone { get; set; }
        public string Fax { get; set; }
        public string Celular { get; set; }
        public string Email { get; set; }
        public string Endereco { get; set; }
        public string Cidade { get; set; }
        public string Estado { get; set; }
        public string ContatoPessoal { get; set; }

    }
}

Signature Interface.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess
{
    public interface IEntidade<T>
    {
        void Inserir(T item);
        void Update(T item);
        void Delete(T item);
        List<T> GetAll();
        T Get(int id);
    }
}

BusinessLayer

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BusinessLayer
{
    class ConnectionHelper
    {
        private SqlConnection connection;
        public ConnectionHelper()
        {
            connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
        }
        public void AttachCommand(SqlCommand command)
        {
            command.Connection = connection;
        }
        public void OpenConnection()
        {
            connection.Open();
        }
        public void FecharConexao()
        {
            connection.Dispose();
        }
    }
}

DataAccess

using BusinessLayer;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess
{
    class DbExecuter
    {
        ConnectionHelper helper = new ConnectionHelper();

        public void Execute(SqlCommand command)
        {
            helper.AttachCommand(command);
            helper.OpenConnection();
            command.ExecuteNonQuery();
            helper.FecharConexao();
        }
        public DataTable Consulta(SqlCommand command)
        {
            helper.AttachCommand(command);
            helper.OpenConnection();
            DataTable dt = new DataTable();
            dt.Load(command.ExecuteReader());
            helper.FecharConexao();
            return dt;
        }

    }
}

Here is my problem, I still do not know how to do select and go to the graphical interface:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MetaData;
using System.Data.SqlClient;

namespace DataAccess
{
    public class FornecedorDAL : IEntidade<Fornecedor>
    {
        DbExecuter exe = new DbExecuter();
        public void Inserir(Fornecedor item)
        {
            SqlCommand cmd = new SqlCommand
           ("INSERT INTO fornecedor VALUES (@nome,@cnpj,@telefone,@fax,@celular,@email,@endereco,@cidade,@estado,@contato_pessoal)");

            cmd.Parameters.AddWithValue("nome", item.Nome);
            cmd.Parameters.AddWithValue("cnpj", item.Cnpj);
            cmd.Parameters.AddWithValue("telefone", item.Telefone);
            cmd.Parameters.AddWithValue("fax", item.Fax);
            cmd.Parameters.AddWithValue("celular", item.Celular);
            cmd.Parameters.AddWithValue("email", item.Email);
            cmd.Parameters.AddWithValue("endereco", item.Endereco);
            cmd.Parameters.AddWithValue("cidade", item.Cidade);
            cmd.Parameters.AddWithValue("estado", item.Estado);
            cmd.Parameters.AddWithValue("contato_pessoal", item.ContatoPessoal);
            exe.Execute(cmd);
        }

        public void Update(Fornecedor item)
        {
            SqlCommand cmd = new SqlCommand("update fornecedor set nome =@nome, telefone = @telefone, fax = @fax, celular = @celular, email = @email, endereco = @endereco, cidade = @cidade, estado = @estado, contato_pessoal = @contato_pessoal where id = @id");         
            cmd.Parameters.AddWithValue("@nome",item.Nome);        
            cmd.Parameters.AddWithValue("@telefone", item.Telefone);
            cmd.Parameters.AddWithValue("@fax", item.Fax);
            cmd.Parameters.AddWithValue("@celular", item.Celular);
            cmd.Parameters.AddWithValue("@email", item.Email);
            cmd.Parameters.AddWithValue("@endereco", item.Endereco);
            cmd.Parameters.AddWithValue("@cidade", item.Cidade);
            cmd.Parameters.AddWithValue("@estado", item.Estado);
            cmd.Parameters.AddWithValue("@contato_pessoal", item.ContatoPessoal);
            cmd.Parameters.AddWithValue("@id", item.Codigo);
            exe.Execute(cmd);    
        }

        public void Delete(Fornecedor item)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "delete from fornecedor where id = @id";
            cmd.Parameters.AddWithValue("@id",item.Codigo);
            exe.Execute(cmd);
        }

        public List<Fornecedor> GetAll()
        {
           Aqui ta meu problema me dar essa força 
        }

        public Fornecedor Get(int id)
        {
            throw new NotImplementedException();
        }
    }
}

Form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetaData;
using DataAccess;
using BusinessLayer;
namespace ControleEstoque
{
    public partial class Fornecedores : Form
    {

        public Fornecedores()
        {
            InitializeComponent();
        }

        private void btnCadastrar_Click(object sender, EventArgs e)
        {
            Fornecedor f = new Fornecedor();
            FornecedorValidar vl = new FornecedorValidar();

            f.Nome = txtNome.Text;
            f.Cnpj = txtCnpj.Text;
            f.Telefone = txtTelefone.Text;
            f.Fax = txtFax.Text;
            f.Celular = txtCelular.Text;
            f.Email = txtEmail.Text;
            f.Endereco = txtEndereco.Text;
            f.Cidade = txtCidade.Text;
            f.Estado = (string)comboEstado.SelectedItem;
            f.ContatoPessoal = txtContatoPessoal.Text;


            try
            {


                vl.Inserir(f);
                MessageBox.Show("ok");
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

        }

        private void btnAtualizar_Click(object sender, EventArgs e)
        {
            Fornecedor f = new Fornecedor();

            f.Codigo = int.Parse(txtCodigo.Text);
            f.Nome = txtNome.Text;
            f.Cnpj = txtCnpj.Text;
            f.Telefone = txtTelefone.Text;
            f.Fax = txtFax.Text;
            f.Celular = txtCelular.Text;
            f.Email = txtEmail.Text;
            f.Endereco = txtEndereco.Text;
            f.Cidade = txtCidade.Text;
            f.Estado = (string)comboEstado.SelectedItem;
            f.ContatoPessoal = txtContatoPessoal.Text;
            FornecedorValidar d = new FornecedorValidar();
            try
            {

                d.Update(f);
                MessageBox.Show("ok");
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

        }

        private void btnExcluir_Click(object sender, EventArgs e)
        {
            Fornecedor f = new Fornecedor();
            f.Codigo = Convert.ToInt32(txtCodigo.Text);
            FornecedorValidar vl = new FornecedorValidar();
            try
            {
                vl.Delete(f);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
    }
}
    
asked by anonymous 07.11.2014 / 23:37

1 answer

1

I believe you can do something like:

public List<Fornecedor> GetAll()
{    
    var listaDeFornecedor = new List<Fornecedor>();
    string sql = "SELECT * FROM Fornecedor";
    using (var cmd = new SqlCommand(sql, connection))
    {
        dt = exe.Consulta(cmd);
        listaDeFornecedor = (from DataRow row in dt.Rows
        select new Fornecedor
        {
            Cnpj = row["Cnpj"].ToString();
            Nome = row["Nome"].ToString();
            //Demais propriedades...       

        }).ToList();         

    }        

    return listaDeFornecedor;
}
    
08.11.2014 / 18:01