using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; namespace CSharpLibrary.SqlTypes { public abstract class Component { public static readonly string strcon = "Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True"; public static SqlCommand select, insert, delete, update; public SqlConnection con = new SqlConnection(strcon); public abstract void Add(); public abstract void Edit(int id, List vlr); public abstract bool Remove(int id); // Retornar valores -- implementados dps... public bool checkLen(int len, int intent) { if (len <= (intent + 1)) return true; return false; } } public abstract class Pessoa : Component { public int? id; public string nome; public string rg; public string cpf; public string tel; public string cel; public string rua; public string num; public string cidade; public string STATUS_PESSOA; protected Pessoa(int? id, string nome, string rg, string cpf, string tel, string cel, string rua, string num, string cidade, string STATUS_PESSOA) { if(id != null) this.id = id; this.nome = checkLen(nome.Length, 50) ? nome : throw new Exception(); this.rg = checkLen(rg.Length, 12) ? cel : throw new Exception(); this.cpf = checkLen(cpf.Length, 11) ? cpf : throw new Exception(); this.tel = checkLen(tel.Length, 10) ? tel : throw new Exception(); this.cel = checkLen(cel.Length, 11) ? cel : throw new Exception(); this.rua = checkLen(rua.Length, 100) ? rua : throw new Exception(); this.num = checkLen(num.Length, 4) ? num : throw new Exception(); this.cidade = checkLen(cidade.Length, 30) ? cidade : throw new Exception(); this.STATUS_PESSOA = STATUS_PESSOA; } } public static class ReturnTypes { // ------------------- Returns Normais ------------------- // public static Autor AReturnAutorByName(string nome) { throw new NotImplementedException(); } public static Emprestimo AReturnEmprestimo(int i) { Emprestimo g = null; using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True")) { con.Open(); Emprestimo.select = new SqlCommand("SELECT * from tbEmprestimo WHERE idEmprestimo = @nome", con); Emprestimo.select.Parameters.Add("@nome", SqlDbType.Int).Value = i; Emprestimo.select.Prepare(); SqlDataReader r2 = Emprestimo.select.ExecuteReader(); while (r2.Read()) { g = new Emprestimo( r2.GetInt32(0), r2.GetInt32(1), r2.GetInt32(2), r2.GetInt32(3), r2.GetDateTime(4), r2.GetDateTime(5), r2.GetString(6) ); } con.Close(); } return g; } public static Item AReturnItem(int i) { Item g = null; using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True")) { con.Open(); Item.select = new SqlCommand("SELECT * from tbItem WHERE idItem = @nome", con); Item.select.Parameters.Add("@nome", SqlDbType.Int).Value = i; Item.select.Prepare(); SqlDataReader r2 = Item.select.ExecuteReader(); while (r2.Read()) { g = new Item(r2.GetInt32(0), r2.GetString(1), r2.GetInt32(2), r2.GetInt32(3), r2.GetInt32(4), r2.GetInt32(5), r2.GetString(6), r2.GetString(7)); } con.Close(); } return g; } public static Cliente AReturnCliente(int i) { Cliente g = null; using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True")) { con.Open(); Cliente.select = new SqlCommand("SELECT * from tbCliente WHERE idCliente = @nome", con); Cliente.select.Parameters.Add("@nome", SqlDbType.Int).Value = i; Cliente.select.Prepare(); SqlDataReader r2 = Cliente.select.ExecuteReader(); while (r2.Read()) { g = new Cliente( r2.GetInt32(0), r2.GetString(1), r2.GetString(2), r2.GetString(3), r2.GetString(4), r2.GetString(5), r2.GetString(6), r2.GetString(7), r2.GetString(8), r2.GetString(9) ); } con.Close(); } return g; } public static Funcionario AReturnFuncionario(int i) { Funcionario g = null; using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True")) { con.Open(); Funcionario.select = new SqlCommand("SELECT * from tbFuncionario WHERE idFunc = @nome", con); Funcionario.select.Parameters.Add("@nome", SqlDbType.Int).Value = i; Funcionario.select.Prepare(); SqlDataReader r2 = Funcionario.select.ExecuteReader(); while (r2.Read()) { g = new Funcionario( r2.GetInt32(0), r2.GetString(1), r2.GetString(2), r2.GetString(3), r2.GetString(4), r2.GetString(5), r2.GetString(6), r2.GetString(7), r2.GetString(8), r2.GetString(9), r2.GetString(10), r2.GetString(11) ); } con.Close(); } return g; } public static Funcionario AReturnFuncionarioByName(string i) { Funcionario g = null; using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True")) { con.Open(); Funcionario.select = new SqlCommand("SELECT * from tbFuncionario WHERE usuarioFunc = @user", con); Funcionario.select.Parameters.Add("@user", SqlDbType.NVarChar,15).Value = i; Funcionario.select.Prepare(); SqlDataReader r2 = Item.select.ExecuteReader(); while (r2.Read()) { g = new Funcionario( r2.GetInt32(0), r2.GetString(1), r2.GetString(2), r2.GetString(3), r2.GetString(4), r2.GetString(5), r2.GetString(6), r2.GetString(7), r2.GetString(8), r2.GetString(9), r2.GetString(10), r2.GetString(11) ); } con.Close(); } return g; } // ------------------- DataSets ------------------- // public static DataSet DSReturnAutorByName(string nome) { DataSet tmp = new DataSet(); using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True")) { con.Open(); Autor.select = new SqlCommand("SELECT * from tbAutor WHERE nomeAutor LIKE @nome", con); Autor.select.Parameters.Add("@nome", SqlDbType.NVarChar).Value = "%" + nome + "%"; Autor.select.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = Autor.select; da.Fill(tmp); con.Close(); } return tmp; } public static DataSet DSReturnGeneroByName(string nome) { DataSet tmp = new DataSet(); using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True")) { con.Open(); Genero.select = new SqlCommand("SELECT * from tbGenero WHERE nomeGenero LIKE @nome", con); Genero.select.Parameters.Add("@nome", SqlDbType.NVarChar).Value = "%" + nome + "%"; Genero.select.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = Genero.select; da.Fill(tmp); con.Close(); } return tmp; } public static DataSet DSReturnClienteByName(string nome) { DataSet tmp = new DataSet(); using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True")) { con.Open(); Cliente.select = new SqlCommand("SELECT * from tbCliente WHERE nomeCliente LIKE @nome", con); Cliente.select.Parameters.Add("@nome", SqlDbType.NVarChar).Value = "%" + nome + "%"; Cliente.select.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = Cliente.select; da.Fill(tmp); con.Close(); } return tmp; } public static DataSet DSReturnFuncionarioByName(string nome) { DataSet tmp = new DataSet(); using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True")) { con.Open(); Funcionario.select = new SqlCommand("SELECT * from tbFuncionario WHERE nomeFunc LIKE @nome", con); Funcionario.select.Parameters.Add("@nome", SqlDbType.NVarChar).Value = "%" + nome + "%"; Funcionario.select.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = Funcionario.select; da.Fill(tmp); con.Close(); } return tmp; } public static DataSet DSReturnTipoByName(string nome) { DataSet tmp = new DataSet(); using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True")) { con.Open(); Item.select = new SqlCommand("SELECT * from tbTipo WHERE nomeTipo LIKE @nome", con); Item.select.Parameters.Add("@nome", SqlDbType.NVarChar).Value = "%" + nome + "%"; Item.select.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = Item.select; da.Fill(tmp); con.Close(); } return tmp; } public static DataSet DSReturnItemByName(string nome) { DataSet tmp = new DataSet(); using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True")) { con.Open(); Item.select = new SqlCommand("SELECT " + "i.idItem, i.nomeItem, a.nomeAutor, g.nomeGenero, e.nomeEditora, t.nomeTipo, i.isbnItem, i.statusItem " + "from tbItem i " + "INNER JOIN tbAutor a ON i.idAutor = a.idAutor " + "INNER JOIN tbGenero g ON i.generoItem = g.idGenero " + "INNER JOIN tbEditora e on i.editoraItem = e.idEditora " + "INNER JOIN tbTipo t on i.tipoItem = t.idTipo " + "WHERE i.nomeItem LIKE @nome", con); Item.select.Parameters.Add("@nome", SqlDbType.NVarChar).Value = "%"+nome+"%"; Item.select.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = Item.select; da.Fill(tmp); con.Close(); } return tmp; } public static DataSet DSReturnEditoraByName(string nome) { DataSet tmp = new DataSet(); using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True")) { con.Open(); Editora.select = new SqlCommand("SELECT * from tbEditora WHERE nomeEditora LIKE @nome", con); Editora.select.Parameters.Add("@nome", SqlDbType.NVarChar).Value = "%" + nome + "%"; Editora.select.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = Editora.select; da.Fill(tmp); con.Close(); } return tmp; } public static DataSet DSReturnEmprestimoByName(string nome) { DataSet tmp = new DataSet(); using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True")) { con.Open(); Emprestimo.select = new SqlCommand("SELECT " + "e.idEmprestimo as #, c.nomeCliente as Cliente, f.nomeFunc as Funcionario, i.nomeItem as Item, e.dataEmprestimo as Emprestado, e.dataDevolucao as Devolvido, e.statusEmprestimo as Estado " + "FROM tbEmprestimo e " + "INNER JOIN tbCliente c ON e.idCliente = c.idCliente " + "INNER JOIN tbFuncionario f ON e.idFunc = f.idFunc " + "INNER JOIN tbItem i on e.idItem = i.idItem " + "WHERE c.nomeCliente LIKE @nomeCliente", con); Emprestimo.select.Parameters.Add("@nomeCliente", SqlDbType.NVarChar, 50).Value = "%" + nome + "%"; Emprestimo.select.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = Emprestimo.select; da.Fill(tmp); con.Close(); } return tmp; } public static DataSet DSReturnEmprestimoAtivo() { DataSet tmp = new DataSet(); using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True")) { con.Open(); Emprestimo.select = new SqlCommand("SELECT " + "e.idEmprestimo as #, c.nomeCliente as Cliente, f.nomeFunc as Funcionario, i.nomeItem as Item, e.dataEmprestimo as Emprestado, e.dataDevolucao as Devolvido, e.statusEmprestimo as Estado " + "FROM tbEmprestimo e " + "INNER JOIN tbCliente c ON e.idCliente = c.idCliente " + "INNER JOIN tbFuncionario f ON e.idFunc = f.idFunc " + "INNER JOIN tbItem i on e.idItem = i.idItem", con); Emprestimo.select.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = Emprestimo.select; da.Fill(tmp); con.Close(); } return tmp; } // ----------------------------------------------------------------------------------------------------------------------- // public static List ReturnEmprestimoVals(string nomeFunc, string nomeCliente, string nomeItem) { List tmp = new List(); using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True")) { con.Open(); SqlCommand tmpSql1 = new SqlCommand("SELECT idFunc from tbFuncionario WHERE nomeFunc = @nomeFunc", con); SqlCommand tmpSql2 = new SqlCommand("SELECT idCliente from tbCliente WHERE nomeCliente = @nomeCliente", con); SqlCommand tmpSql3 = new SqlCommand("SELECT idItem from tbItem WHERE nomeItem = @nomeItem", con); tmpSql1.Parameters.Add("@nomeFunc", SqlDbType.NVarChar, 50).Value = nomeFunc; tmpSql1.Prepare(); SqlDataReader r1 = tmpSql1.ExecuteReader(); while (r1.Read()) tmp.Add(r1.GetInt32(0)); r1.Close(); tmpSql2.Parameters.Add("@nomeCliente", SqlDbType.NVarChar, 50).Value = nomeCliente; tmpSql2.Prepare(); SqlDataReader r2 = tmpSql2.ExecuteReader(); while (r2.Read()) tmp.Add(r2.GetInt32(0)); r2.Close(); tmpSql3.Parameters.Add("@nomeItem", SqlDbType.NVarChar, 50).Value = nomeItem; tmpSql3.Prepare(); SqlDataReader r3 = tmpSql3.ExecuteReader(); while (r3.Read()) tmp.Add(r3.GetInt32(0)); r3.Close(); con.Close(); } return tmp; } public static List ReturnItemVals(string Autor, string Genero, string Editora, string Tipo) { List tmp = new List(); using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True")) { con.Open(); SqlCommand tmpSql1 = new SqlCommand("SELECT idAutor from tbAutor WHERE nomeAutor = @nomeAutor", con); SqlCommand tmpSql2 = new SqlCommand("SELECT idGenero from tbGenero WHERE nomeGenero = @nomeGenero", con); SqlCommand tmpSql3 = new SqlCommand("SELECT idEditora from tbEditora WHERE nomeEditora = @nomeEditora", con); SqlCommand tmpSql4 = new SqlCommand("SELECT idTipo from tbTipo WHERE nomeTipo = @nomeTipo", con); tmpSql1.Parameters.Add("@nomeAutor", SqlDbType.NVarChar, 200).Value = Autor; tmpSql1.Prepare(); SqlDataReader r1 = tmpSql1.ExecuteReader(); while (r1.Read()) tmp.Add(r1.GetInt32(0)); r1.Close(); tmpSql2.Parameters.Add("@nomeGenero", SqlDbType.NVarChar, 30).Value = Genero; tmpSql2.Prepare(); SqlDataReader r2 = tmpSql1.ExecuteReader(); while (r2.Read()) tmp.Add(r2.GetInt32(0)); r2.Close(); tmpSql3.Parameters.Add("@nomeEditora", SqlDbType.NVarChar, 30).Value = Editora; tmpSql3.Prepare(); SqlDataReader r3 = tmpSql1.ExecuteReader(); while (r3.Read()) tmp.Add(r3.GetInt32(0)); r3.Close(); tmpSql4.Parameters.Add("@nomeTipo", SqlDbType.NVarChar, 30).Value = Tipo; tmpSql4.Prepare(); SqlDataReader r4 = tmpSql1.ExecuteReader(); while (r4.Read()) tmp.Add(r4.GetInt32(0)); r4.Close(); con.Close(); } return tmp; } public static List> ReturnEmprestimoByName(string nomeCliente) { List> tmp = new List>(); using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True")) { con.Open(); Emprestimo.select = new SqlCommand("SELECT " + "e.idEmprestimo, f.nomeFunc, c.nomeCliente, i.nomeItem, e.dataEmprestimo, e.dataDevolucao, e.statusEmprestimo " + "FROM tbEmprestimo e " + "INNER JOIN tbCliente c ON e.idCliente = c.idCliente " + "INNER JOIN tbFuncionario f ON e.idFunc = f.idFunc " + "INNER JOIN tbItem i on e.idItem = i.idItem " + "WHERE c.nomeCliente LIKE @nomeCliente", con); Emprestimo.select.Parameters.Add("@nomeCliente", SqlDbType.NVarChar, 50).Value = "%" + nomeCliente + "%"; Emprestimo.select.Prepare(); using (SqlDataReader r = Emprestimo.select.ExecuteReader()) { while (r.Read()) { tmp.Add( new Tuple ( r.GetInt32(0), r.GetString(1), r.GetString(2), r.GetString(3), r.GetDateTime(4), r.GetDateTime(5), r.GetString(6) ) ); } } con.Close(); } return tmp; } // * Verificar Login * // public static bool CheckLogin(string USER, string PASSWD) { try { SqlConnection con = new SqlConnection("Data Source=DESKTOP-PPL6NEP;Initial Catalog=tbTrabalho;Integrated Security=True"); con.Open(); List < Tuple> a = new List>(); int i = 0; Funcionario.select = new SqlCommand("SELECT usuarioFunc, senhaFunc FROM " + "tbFuncionario", con); using (SqlDataReader r = Funcionario.select.ExecuteReader()) { while(r.Read()) { a.Add( new Tuple ( r.GetString(0), r.GetString(1) ) ); } } con.Close(); foreach(Tuple e in a) { if(e.Item1 == USER) { i++; } if (e.Item2 == PASSWD) { i++; } } if (i == 2) return true; } catch (SqlException) { throw new Exception(); } return false; } } }