Exemple #1
0
        private void Alterar()
        {
            Peixes peixes = new Peixes();

            peixes.Id         = Convert.ToInt32(lblId.Text);
            peixes.Nome       = lblNome.Text;
            peixes.Raca       = cbRaca.Text;
            peixes.Preco      = Convert.ToDecimal(lblPreco.Text);
            peixes.Quantidade = Convert.ToInt32(lblQuantidade.Text);

            SqlConnection conexao = new SqlConnection();

            conexao.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=T:\Documentos\ExercicioBancoDados.mdf;Integrated Security=True;Connect Timeout=30";
            conexao.Open();

            SqlCommand comando = new SqlCommand();

            comando.Connection  = conexao;
            comando.CommandText = @"UPDATE peixes SET
    nome = @NOME,
    raca = @RACA,
    preco = @PRECO,
    quantidade = @QUANTIDADE
WHERE id = @ID";
            comando.Parameters.AddWithValue("@ID", peixes.Id);
            comando.Parameters.AddWithValue("@NOME", peixes.Nome);
            comando.Parameters.AddWithValue("@RACA", peixes.Raca);
            comando.Parameters.AddWithValue("@PRECO", peixes.Preco);
            comando.Parameters.AddWithValue("@QUANTIDADE", peixes.Quantidade);
            comando.ExecuteNonQuery();
            conexao.Close();

            AtualizarTabela();
            LimparCampos();
        }
Exemple #2
0
        private void Inserir()
        {
            Peixes peixe = new Peixes();

            peixe.Id         = Convert.ToInt32(lblId.Text);
            peixe.Nome       = txtNome.Text;
            peixe.Raca       = cbRaca.Text;
            peixe.Preco      = Convert.ToDecimal(mtbPreco.Text.Replace("R$", "").Replace("$", ""));
            peixe.Quantidade = Convert.ToInt32(nudQuantidade.Text);

            SqlConnection conexao = new SqlConnection();

            conexao.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=T:\Documentos\ExercicioBancoDados.mdf;Integrated Security=True;Connect Timeout=30";
            conexao.Open();

            SqlCommand comando = new SqlCommand();

            comando.Connection  = conexao;
            comando.CommandText = @"INSERT INTO peixes 
(nome, raca, preco, quantidade)
VALUES(@NOME, @RACA, @PRECO, @QUANTIDADE)";
            comando.Parameters.AddWithValue("@NOME", peixe.Nome);
            comando.Parameters.AddWithValue("@RACA", peixe.Raca);
            comando.Parameters.AddWithValue("@PRECO", peixe.Preco);
            comando.Parameters.AddWithValue("@QUANTIDADE", peixe.Quantidade);
            comando.ExecuteNonQuery();
            MessageBox.Show("Registro criado com sucesso!");

            LimparCampos();
            conexao.Close();
            AtualizarTabela();
        }
Exemple #3
0
        private void AtualizarTabela()
        {
            SqlConnection conexao = new SqlConnection();

            conexao.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=T:\Documentos\ExercicioBancoDados.mdf;Integrated Security=True;Connect Timeout=30";
            conexao.Open();

            SqlCommand comando = new SqlCommand();

            comando.Connection  = conexao;
            comando.CommandText = @"SELECT id, nome, raca, preco, quantidade FROM peixes";

            DataTable tabela = new DataTable();

            tabela.Load(comando.ExecuteReader());

            dgvPeixes.RowCount = 0;
            for (int i = 0; i < tabela.Rows.Count; i++)
            {
                DataRow linha = tabela.Rows[i];
                Peixes  peixe = new Peixes();
                peixe.Id         = Convert.ToInt32(linha["id"]);
                peixe.Nome       = linha["nome"].ToString();
                peixe.Raca       = linha["raca"].ToString();
                peixe.Preco      = Convert.ToDecimal(linha["preco"].ToString());
                peixe.Quantidade = Convert.ToInt32(linha["quantidade"].ToString());
                dgvPeixes.Rows.Add(new string[]
                {
                    peixe.Id.ToString(), peixe.Nome.ToString(), peixe.Raca.ToString(), peixe.Preco.ToString(), peixe.Quantidade.ToString()
                });
            }

            conexao.Close();
        }