2016-10-10 5 views
-2

私が挿入ボタンをクリックすると、私のプログラムは私にCPFの近くにSQL構文エラーがあると教えてくれるが、エラーは190890と同じようにテキストボックスの中央で始まるテキストボックスやプログラムで0.567から45のエラーが近いことを私に告げる「567から45には、」、ここでのコードは次のとおりです。エラーテキストボックスの真ん中のSQL構文

private void btninserir_Click(object sender, EventArgs e) 
    { 

     string sql; 
     int numero; 

     sql = "select * from doador where CPF = " + txtCPF.Text; 
     DataTable dt = bd.executarconsulta(sql); 

     if (dt.Rows.Count > 0) 
     { 
      MessageBox.Show("Doador já cadastrado!!!!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
     } 
     else 
     { 

      if (txtNome.Text != "" || txtCel.Text != "" || txtCPF.Text != "" || txtEndereco.Text != "" || txtTelRe.Text != "" || int.TryParse(txtIdade.Text, out numero)) 
      { 
       bd.ConectarBD(); 
       sql = "INSERT INTO DOADOR(CPF, NOME, IDADE, TELCASA, TELCELULAR, ENDERECO) VALUES('" + txtCPF.Text + "','" + txtNome.Text + "'," + txtIdade.Text + ",'" + txtTelRe.Text + "','" + txtCel.Text + "','" + txtEndereco.Text + "')"; 
       bd.executarcomandos(sql); 
       MessageBox.Show("Doador Cadastro com sucesso!!!!", "", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      } 
      else 
      { 
       MessageBox.Show("Valor Invalido!!!!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       txtCPF.BackColor = Color.Red; 
       txtNome.BackColor = Color.Red; 
       txtEndereco.BackColor = Color.Red; 
       txtIdade.BackColor = Color.Red; 
       txtTelRe.BackColor = Color.Red; 
       txtCel.BackColor = Color.Red; 
       txtCPF.Focus(); 
      } 

     } 
    } 

Error message

+3

文字列連結を使用しないでください!パラメータを使用してください! – Dai

+0

どのようなエラーが返されますか? –

+0

テキストボックス文字列間の区切り文字はすべて間違っています。 – slugster

答えて

-3

ただ、SQLでのテキストのparamの間'を追加します。このように:
sql = "select * from doador where CPF = '" + txtCPF.Text + "'";

+0

は既にそれを持っています –

+1

本当に良い解決策ではありません。代わりにパラメータを使用することを提案します。 –

+0

パラマターの使い方は? –