/// <summary> /// recolhe todos gnr da bd /// </summary> /// <param name="id"></param> /// <returns></returns> public List <Gnr> GetAllGnr() { List <Gnr> list = new List <Gnr>(); SqlDataReader rdr = null; OpenDB(); // create a command object SqlCommand cmd = new SqlCommand("select * from Gnr", cnn); rdr = cmd.ExecuteReader(); while (rdr.Read()) { Gnr g = new Gnr(); g.Id = (int)rdr["GndID"]; g.PNome = (string)rdr["PNome"]; g.UNome = (string)rdr["UNome"]; g.DataNasc = (DateTime)rdr["DataNasc"]; g.Numero = (int)rdr["Numero"]; g.Ativo = (bool)rdr["Ativo"]; list.Add(g); } CloseDB(); return(list); }
/// <summary> /// Envia um gnr /// </summary> /// <returns></returns> public Gnr GetGnr(string id) { int newId; if (!Int32.TryParse(id, out newId)) { return(null); } SqlDataReader rdr = null; OpenDB(); // create a command object SqlCommand cmd = new SqlCommand("select * from Gnr", cnn); rdr = cmd.ExecuteReader(); while (rdr.Read()) { if (newId == (int)rdr["GndID"]) { Gnr g = new Gnr(); g.Id = (int)rdr["GndID"]; g.PNome = (string)rdr["PNome"]; g.UNome = (string)rdr["UNome"]; g.DataNasc = (DateTime)rdr["DataNasc"]; g.Numero = (int)rdr["Numero"]; g.Ativo = (bool)rdr["Ativo"]; return(g); } } CloseDB(); return(null); }
/// <summary> /// Metodo permite fazer update a um gnr /// </summary> /// <param name="gnr"></param> /// <returns></returns> public bool UpdateGnr(Gnr gnr) { StringBuilder query = new StringBuilder(); SqlDataReader rdr = null; OpenDB(); query.AppendFormat("UPDATE Gnr SET PNome='{0}', UNome='{1}', DataNasc='{2}', Numero={3}, Ativo={4} WHERE GndID={5}" , gnr.PNome, gnr.UNome, gnr.DataNasc.ToString("yyyy-MM-dd"), gnr.Numero, Convert.ToInt32(gnr.Ativo), gnr.Id); // create a command object SqlCommand cmd = new SqlCommand(query.ToString(), cnn); rdr = cmd.ExecuteReader(); CloseDB(); return(true); }
/// <summary> /// Metodo permito add um gnr /// </summary> /// <param name="gnr"></param> /// <returns></returns> public bool AddGnr(Gnr gnr) { StringBuilder query = new StringBuilder(); SqlDataReader rdr = null; OpenDB(); query.AppendFormat("INSERT INTO Gnr (GndID, PNome, UNome, DataNasc, Numero, Ativo) VALUES ({0}, '{1}', '{2}', '{3}', {4}, {5});" , gnr.Id, gnr.PNome, gnr.UNome, gnr.DataNasc.ToString("yyyy-MM-dd"), gnr.Numero, Convert.ToInt32(gnr.Ativo)); // create a command object SqlCommand cmd = new SqlCommand(query.ToString(), cnn); rdr = cmd.ExecuteReader(); CloseDB(); return(true); }