public T SearchById(int id) { try { db = new DBMySQL(); var campos = typeof(T).GetProperties(); Type Ttypo = typeof(T); string sql = "SELECT * FROM " + typeof(T).Name + " WHERE " + campos[0].Name + "="; switch (Ttypo.GetProperty(campos[0].Name).PropertyType.Name) { case "String": sql += "'" + id + "'"; break; default: sql += " " + id; break; } MySqlDataReader r = (MySqlDataReader)db.Consulta(sql); T dato = (T)Activator.CreateInstance(typeof(T)); int j = 0; while (r.Read()) { dato = (T)Activator.CreateInstance(typeof(T)); for (int i = 0; i < campos.Length; i++) { PropertyInfo prop = Ttypo.GetProperty(campos[i].Name); prop.SetValue(dato, r[i]); } j++; } r.Close(); db.CerrarConexion(); if (j > 0) { Error = ""; return(dato); } else { Error = "Id no existente..."; return(null); } } catch (Exception ex) { Error = ex.Message; return(null); } }
private bool EjecutarComando(string sql) { db = new DBMySQL(); if (db.Comando(sql)) { Error = ""; db.CerrarConexion(); return(true); } else { Error = db.Error; db.CerrarConexion(); return(false); } }