Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE perguntas (
- cod_pergunta SERIAL PRIMARY KEY NOT NULL,
- pergunta VARCHAR(500),
- opcao_um VARCHAR(500),
- opcao_dois VARCHAR(500),
- opcao_tres VARCHAR(500),
- opcao_quatro VARCHAR(500),
- opcao_correta INTEGER,
- IDcategoria INTEGER,
- CONSTRAINT fk_categoria FOREIGN KEY (IDcategoria) REFERENCES categoria(cod_categoria)
- );
- CREATE TABLE categoria (
- cod_categoria SERIAL PRIMARY KEY NOT NULL,
- categoria VARCHAR(15),
- descricao VARCHAR(140)
- );
- private void btnGravar_Click(object sender, EventArgs e)
- {
- //Verifica qual radio button está selecionado
- int valor;
- valor = 0;
- if (rbCorreta1.Checked == true)
- valor = 1;
- else if (rbCorreta2.Checked == true)
- valor = 2;
- else if (rbCorreta3.Checked == true)
- valor = 3;
- else if (rbCorreta4.Checked == true)
- valor = 4;
- else
- MessageBox.Show("Selecione a resposta correta!");
- //Verifica qual o valor do combobox está selecionado e guarda o ID para gravar
- string IndexSelecionado = cbCategoria.SelectedIndex.ToString();
- string str = "Host=127.0.0.1;Username=postgres;Password=adm;Database=dbquiz";
- string gravarsql = "INSERT INTO perguntas (pergunta, opcao_um, opcao_dois, opcao_tres, opcao_quatro, opcao_correta, idcategoria) " + " VALUES ('" + txtPergunta.Text + "', '" + txtResposta1.Text + "', '" + txtResposta2.Text + "', '" + txtResposta3.Text + "', '" + txtResposta4.Text + "', '" + valor + "', '"+ IndexSelecionado + "');";
- Npgsql.NpgsqlConnection con = new Npgsql.NpgsqlConnection(str);
- Npgsql.NpgsqlCommand cmd = new Npgsql.NpgsqlCommand(gravarsql, con);
- cmd.CommandType = CommandType.Text;
- con.Open();
- try
- {
- int n = cmd.ExecuteNonQuery();
- if (n > 0)
- {
- MessageBox.Show("Efetuado!");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error: " + ex.ToString());
- }
- finally
- {
- con.Close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement