コード例 #1
0
        public void cadastraCombustivel(ModeloCombustivel modeloCombustivel)
        {
            try
            {
                cn = new MySqlConnection(conexao.conectar());

                cn.Open();

                //string inserir = "INSERT INTO tbcombustivel(combustivel)VALUES(" + "'" + modeloCombustivel.Combustivel + "')";
                //comando = new MySqlCommand(inserir, cn);

                comando             = new MySqlCommand("pinserirCombustivel", cn);
                comando.CommandType = CommandType.StoredProcedure;

                comando.Parameters.AddWithValue("Combustivel", modeloCombustivel.Combustivel);


                comando.ExecuteNonQuery();


                cn.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("Erro de Banco" + ex);
            }
            finally
            {
                cn.Close();
            }
        }
        private void btnCadCombustivel_Click(object sender, EventArgs e)
        {
            ModeloCombustivel modeloCombustivel = new ModeloCombustivel();
            Cadastro cadastro = new Cadastro();

            if (string.IsNullOrEmpty(txtCombustivel.Text))
            {
                MessageBox.Show("O Combustível é obrigatório!", "ATENÇÃO",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);

                txtCombustivel.Focus();
            }
            else
            {
                modeloCombustivel.Combustivel = txtCombustivel.Text;

                MessageBox.Show("Combustível cadastrado com sucesso!", "Combustível Cadastrado!",
                   MessageBoxButtons.OK, MessageBoxIcon.Information);

                txtCombustivel.Clear();

                cadastro.cadastraCombustivel(modeloCombustivel);

                var resposta = MessageBox.Show("Dados cadastrados com Sucesso. " +
                   "Deseja adicionar outro ?", "Novo Registro", MessageBoxButtons.YesNo,
                   MessageBoxIcon.Exclamation);

                if (resposta == DialogResult.Yes)
                {
                    txtCombustivel.Clear();
                }
                else
                {
                    this.Close();
                }
            }
        }