Advertisement
Guest User

Untitled

a guest
Aug 30th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. CREATE TABLE perguntas (
  2. cod_pergunta SERIAL PRIMARY KEY NOT NULL,
  3. pergunta VARCHAR(500),
  4. opcao_um VARCHAR(500),
  5. opcao_dois VARCHAR(500),
  6. opcao_tres VARCHAR(500),
  7. opcao_quatro VARCHAR(500),
  8. opcao_correta INTEGER,
  9. IDcategoria INTEGER,
  10. CONSTRAINT fk_categoria FOREIGN KEY (IDcategoria) REFERENCES categoria(cod_categoria)
  11. );
  12.  
  13. CREATE TABLE categoria (
  14. cod_categoria SERIAL PRIMARY KEY NOT NULL,
  15. categoria VARCHAR(15),
  16. descricao VARCHAR(140)
  17. );
  18.  
  19. private void btnGravar_Click(object sender, EventArgs e)
  20. {
  21. //Verifica qual radio button está selecionado
  22. int valor;
  23. valor = 0;
  24. if (rbCorreta1.Checked == true)
  25. valor = 1;
  26. else if (rbCorreta2.Checked == true)
  27. valor = 2;
  28. else if (rbCorreta3.Checked == true)
  29. valor = 3;
  30. else if (rbCorreta4.Checked == true)
  31. valor = 4;
  32. else
  33. MessageBox.Show("Selecione a resposta correta!");
  34.  
  35.  
  36. //Verifica qual o valor do combobox está selecionado e guarda o ID para gravar
  37. string IndexSelecionado = cbCategoria.SelectedIndex.ToString();
  38.  
  39.  
  40.  
  41. string str = "Host=127.0.0.1;Username=postgres;Password=adm;Database=dbquiz";
  42. 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 + "');";
  43. Npgsql.NpgsqlConnection con = new Npgsql.NpgsqlConnection(str);
  44. Npgsql.NpgsqlCommand cmd = new Npgsql.NpgsqlCommand(gravarsql, con);
  45. cmd.CommandType = CommandType.Text;
  46. con.Open();
  47.  
  48. try
  49. {
  50. int n = cmd.ExecuteNonQuery();
  51. if (n > 0)
  52. {
  53. MessageBox.Show("Efetuado!");
  54. }
  55.  
  56. }
  57. catch (Exception ex)
  58. {
  59. MessageBox.Show("Error: " + ex.ToString());
  60. }
  61. finally
  62. {
  63. con.Close();
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement