Пример #1
0
        private void mostraResultados()
        {
            MySql           instanciaMysql = new MySql();
            MySqlConnection conexao        = instanciaMysql.GetConnection();

            try
            {
                conexao.Open();
                string sql = "SELECT Produto.idProduto as Código, " +
                             "Produto.descricao as Descrição, " +
                             "Produto.tipoUnitario as Tipo, " +
                             "Categoria_Produto.descricao as Categoria, " +
                             "Produto.codBarra as Código_de_Barras, " +
                             "Produto.valor as Valor FROM Produto " +
                             "inner join Categoria_Produto " +
                             "on Produto.idCategoria = Categoria_Produto.idCategoria ORDER BY idProduto";
                MySqlCommand     cmd = new MySqlCommand(sql, conexao);
                MySqlDataAdapter da  = new MySqlDataAdapter(cmd);
                DataSet          ds  = new DataSet();

                //preenche o dataset através do adapter
                da.Fill(ds, "Produto");
                //atribui o resultado à propriedade DataSource da dataGridView
                grid.DataSource = ds;
                grid.DataMember = "Produto";
                grid.Columns["Valor"].DefaultCellStyle.Format = "c2";
            }
            catch (Exception erro)
            {
                throw erro;
            }
            finally
            {
                conexao.Close();
                limparCampos();
            }
        }
Пример #2
0
        private void salvar_Click(object sender, EventArgs e)
        {
            this.salvar.Visible = false;
            this.cancelar.Visible = false;
            MySql instanciaMysql = new MySql();
            MySqlConnection conexao = instanciaMysql.GetConnection();
            if (MessageBox.Show("Deseja alterar o registro?", "Alteração", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                        conexao.Open();
                        string sql = $"update Categoria_Produto set descricao='{descricao.Text}' WHERE idCategoria = @param1";

                        MySqlCommand cmd = new MySqlCommand(sql, conexao);
                        cmd.Parameters.Add(new MySqlParameter("@param1", grid.CurrentRow.Cells[0].Value));
                        cmd.CommandType = CommandType.Text;
                        cmd.ExecuteNonQuery();
                }
                catch (Exception erro)
                {
                    throw erro;
                }
                finally
                {
                    conexao.Close();
                    mostraResultados();
                    limparCampos();
                    MessageBox.Show("Salvo com Sucesso!");
                    this.editar.Visible = true;
                    this.cadastrar.Visible = true;
                    this.apagar.Visible = true;
                }
             
            }
           
            
        }
Пример #3
0
        private void salvar_Click(object sender, EventArgs e)
        {
            this.salvar.Visible   = false;
            this.cancelar.Visible = false;


            if (MessageBox.Show("Deseja alterar o registro?", "Alteração", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                MySql instanciaMysql = new MySql();

                try
                {
                    using (MySqlConnection conexao = instanciaMysql.GetConnection())
                    {
                        conexao.Open();
                        int          codigoCategoria = Convert.ToInt32(((DataRowView)categoria.SelectedItem)["idCategoria"]);
                        string       sql             = $"update Produto set descricao='{descricao.Text}',tipoUnitario='{tipo.Text}',idCategoria='{codigoCategoria}',valor='{valor.Text}' WHERE idProduto = @param1";
                        MySqlCommand cmd             = new MySqlCommand(sql, conexao);
                        cmd.Parameters.Add(new MySqlParameter("@param1", grid.CurrentRow.Cells[0].Value));
                        cmd.CommandType = CommandType.Text;
                        cmd.ExecuteNonQuery();
                        conexao.Close();
                        mostraResultados();
                        limparCampos();
                        MessageBox.Show("Salvo com Sucesso!");
                        this.editar.Visible        = true;
                        this.cadastrar.Visible     = true;
                        this.apagar.Visible        = true;
                        this.gerarEtiqueta.Visible = true;
                    }
                }
                catch (Exception erro)
                {
                    throw erro;
                }
            }
        }