Beispiel #1
0
        public static List <Cliente> SelectAll()
        {
            Cliente        c;
            List <Cliente> cs = new List <Cliente>();

            try
            {
                using (conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    string        s   = "SELECT * FROM Cliente";
                    SqlCommand    cmd = new SqlCommand(s, conn);
                    SqlDataReader d;
                    using (d = cmd.ExecuteReader())
                    {
                        if (d.HasRows)
                        {
                            while (d.Read())
                            {
                                int      i    = Convert.ToInt32(d["id"]);
                                string   n    = d["Nome"].ToString();
                                string   cpf  = (d["CPF"] != null) ? d["CPF"].ToString() : "";
                                string   cnpj = (d["CNPJ"] != null) ? d["CNPJ"].ToString() : "";
                                string   e    = d["Email"].ToString();
                                Endereco end  = DALEndereco.SelectFromCliente(i);
                                Telefone t    = DALTelefone.SelectFromCliente(i);
                                c = new Cliente(i, n, e, end, t);
                                if (cpf != "")
                                {
                                    c.setCPF(cpf);
                                }
                                else
                                {
                                    c.setCNPJ(cnpj);
                                }
                                cs.Add(c);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(cs);
        }
Beispiel #2
0
        public static Cliente Select(int i)
        {
            Cliente c = new Cliente();

            try
            {
                using (conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    string     s   = "SELECT * FROM Cliente WHERE id = @id ";
                    SqlCommand cmd = new SqlCommand(s, conn);
                    cmd.Parameters.Add("@id", SqlDbType.Int).Value = i;
                    SqlDataReader d;
                    using (d = cmd.ExecuteReader())
                    {
                        if (d.HasRows)
                        {
                            while (d.Read())
                            {
                                string   n    = d["Nome"].ToString();
                                string   cpf  = (d["CPF"] != null) ? d["CPF"].ToString() : "";
                                string   cnpj = (d["CNPJ"] != null) ? d["CNPJ"].ToString() : "";
                                string   e    = d["Email"].ToString();
                                Endereco end  = DALEndereco.SelectFromCliente(i);
                                Telefone t    = DALTelefone.SelectFromCliente(i);
                                c = new Cliente(i, n, e, end, t);
                                if (cpf != "")
                                {
                                    c.setCPF(cpf);
                                }
                                else
                                {
                                    c.setCNPJ(cnpj);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(c);
        }