public static PerfilClassOnline saveProfile(PerfilClassOnline p) { MySqlConnection conexion = null; try { conexion = getConnection(); conexion.Open(); MySqlTransaction myTrans = conexion.BeginTransaction(); MySqlCommand comando = new MySqlCommand("SELECT id FROM Perfil WHERE nombre=@nombre and fk_Usuario=@usuario", conexion); comando.Parameters.AddWithValue("@nombre", p.nombre); comando.Parameters.AddWithValue("@usuario", VIGallery.getUser().id); MySqlDataReader reader = comando.ExecuteReader(); if (!reader.HasRows) { reader.Close(); comando = new MySqlCommand("INSERT INTO Perfil VALUES (null, @nombrePerfil, @mode, @numMenus, @fkUsuario)", conexion); comando.Transaction = myTrans; comando.Parameters.AddWithValue("@nombrePerfil", p.nombre); comando.Parameters.AddWithValue("@mode", 0); comando.Parameters.AddWithValue("@numMenus", 0); comando.Parameters.AddWithValue("@fkUsuario", VIGallery.getUser().id); comando.ExecuteNonQuery(); myTrans.Commit(); comando.Parameters.Clear(); reader.Close(); comando = new MySqlCommand("SELECT id FROM Perfil WHERE nombre=@nombre and fk_Usuario=@fkUsuario", conexion); comando.Parameters.AddWithValue("@nombre", p.nombre); comando.Parameters.AddWithValue("@fkUsuario", p.idUsuario); reader = comando.ExecuteReader(); if (reader.HasRows) { reader.Read(); Int32 id = (Int32)reader["id"]; p.id = id; return(p); } return(p); } } catch (MySqlException e) { Console.WriteLine("No se ha podido añadir el perfil:\n" + e); throw e; } finally { if (conexion != null) { conexion.Close(); } } return(null); }
public static List <PerfilClass> loadProfiles(long id) { List <PerfilClass> perfiles = null; MySqlConnection conexion = null; try { conexion = getConnection(); conexion.Open(); MySqlTransaction myTrans = conexion.BeginTransaction(); MySqlCommand comando = new MySqlCommand("SELECT id,nombre,numMenus,mode FROM Perfil WHERE fk_Usuario=@idUsuario", conexion); comando.Parameters.AddWithValue("@idUsuario", id); MySqlDataReader reader = comando.ExecuteReader(); if (reader.HasRows) { perfiles = new List <PerfilClass>(); while (reader.Read()) { Int32 idPerfil = (Int32)reader["id"]; Int32 numMenus = (Int32)reader["numMenus"]; Int32 mode = (Int32)reader["mode"]; PerfilClassOnline perfil = new PerfilClassOnline((long)idPerfil, reader["nombre"].ToString(), mode, (long)numMenus, id); perfiles.Add(perfil); } reader.Close(); } } catch (MySqlException e) { Console.WriteLine("Error al cargar los perfiles:\n" + e); throw e; } finally { if (conexion != null) { conexion.Close(); } } return(perfiles); }