コード例 #1
0
        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);
        }
コード例 #2
0
        public void onAceptar(object sender, RoutedEventArgs e)
        {
            if (newProfileText.Text.CompareTo("") != 0)
            {
                Regex containsABadCharacter = new Regex("[" + Regex.Escape(new string(System.IO.Path.GetInvalidFileNameChars())) + "]");
                if (!containsABadCharacter.IsMatch(newProfileText.Text))
                {
                    if (!Lista.checkProfile(newProfileText.Text))
                    {
                        PerfilClassOnline pfOnline = new PerfilClassOnline(newProfileText.Text.ToString(), VIGallery.getUser().id);
                        pfOnline = Conexion.saveProfile(pfOnline);

                        if (pfOnline != null)
                        {
                            addedProfile = true;
                            Lista.addProfile(pfOnline);
                        }
                        name = newProfileText.Text;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("El perfil ya existe");
                    }
                }
                else
                {
                    MessageBox.Show("El nombre contiene caractéres no permitidos: " + new string(System.IO.Path.GetInvalidFileNameChars()));
                }
            }
            else
            {
                MessageBox.Show("No has introducido un nombre");
            }
        }