public Carro Read(int key) { Database db = Database.GetInstance(); string qry = String.Format("SELECT * FROM carro WHERE Id = " + key); DataSet ds = db.ExecuteQuery(qry); Carro c = new Carro(); DataRow dr = ds.Tables[0].Rows[0]; c.Id = int.Parse(dr["id"].ToString()); c.Nome = dr["nome"].ToString(); c.Modelo = dr["modelo"].ToString(); c.PrecoBase = double.Parse(dr["precobase"].ToString()); c.Removido = int.Parse(dr["removido"].ToString()); //junta carro com motor int id; MotorDAO motor = new MotorDAO(); qry = String.Format("SELECT m.id, m.nome, m.ano, m.cavalos, m.litragem, m.preco, m.removido FROM carro c JOIN carromotor cm ON c.id = cm.idcarro JOIN motor m ON cm.idmotor = m.id WHERE c.id ={0}", key); ds = db.ExecuteQuery(qry); foreach (DataRow drmotor in ds.Tables[0].Rows) { Motor m = new Motor(); m.Id = int.Parse(drmotor["Id"].ToString()); m.Nome = drmotor["nome"].ToString(); m.Ano = drmotor["ano"].ToString(); m.Cavalos = drmotor["cavalos"].ToString(); m.Litragem = drmotor["Litragem"].ToString(); m.PrecoBase = double.Parse(drmotor["preco"].ToString()); m.Removido = int.Parse(drmotor["removido"].ToString()); c.Motor.Add(m); } //junta carro com opcionais qry = "SELECT o.id, o.nome, o.ano, o.itens, o.preco, o.removido FROM carro c JOIN carroOpcional co ON c.id = co.idcarro JOIN opcional o ON co.idopcional = o.id WHERE c.id = " + key; ds = db.ExecuteQuery(qry); foreach (DataRow dropcional in ds.Tables[0].Rows) { Opcional o = new Opcional(); o.Id = int.Parse(dropcional["Id"].ToString()); o.Nome = dropcional["nome"].ToString(); o.Ano = dropcional["ano"].ToString(); o.Itens = dropcional["itens"].ToString(); o.Preco = double.Parse(dropcional["preco"].ToString()); o.Removido = int.Parse(dropcional["removido"].ToString()); c.Opcional.Add(o); } return(c); }
public List <Motor> Read(int key) { MotorDAO motor = new MotorDAO(); Database db = Database.GetInstance(); string qry = String.Format("SELECT m.id, m.nome, m.ano, m.cavalos, m.litragem, m.preco, m.removido FROM carro c JOIN carromotor cm ON c.id = cm.idcarro JOIN motor m ON cm.idmotor = m.id WHERE c.id ={0}", key); DataSet ds = db.ExecuteQuery(qry); List <Motor> lm = new List <Motor>(); foreach (DataRow drmotor in ds.Tables[0].Rows) { Motor m = new Motor(); m.Id = int.Parse(drmotor["Id"].ToString()); m.Nome = drmotor["nome"].ToString(); m.Ano = drmotor["ano"].ToString(); m.Cavalos = drmotor["cavalos"].ToString(); m.Litragem = drmotor["Litragem"].ToString(); m.PrecoBase = double.Parse(drmotor["preco"].ToString()); m.Removido = int.Parse(drmotor["removido"].ToString()); lm.Add(m); } return(lm); }