2016-06-24 9 views
0

従業員が更新する必要があるデータベースにCRMテーブルがあります。私はWindowsフォームを使用してSQL接続を使用してそれらをリンクしていますが、sytanxがキー 'In'の近くで間違っているというエラーが発生し続けています。あなたは閉じ括弧を配置すると、カラムの値のための「'」文字を置くのを忘れC#でSQL接続を使用してテーブルにデータを追加

string sql = "INSERT INTO Trafico (Nombre, Apedillo, Correo, Teléfono, Como, Comercial, Tipo, Contacto,Inteserado En) Values('" + nombreTextBox.Text + "','" + apedilloTextBox.Text + "','" + correoTextBox.Text + "','" + teléfonoTextBox.Text + "','" + comoComboBox.Text + "','" + comercialComboBox.Text + "','" + tipoComboBox.Text + "','" + contactoComboBox.Text + "','" + inteserado_EnComboBox.Text + "')"; 

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : MetroFramework.Forms.MetroForm 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      // TODO: This line of code loads data into the 'traficoDataSet.Inteserado_En' table. You can move, or remove it, as needed. 
      this.inteserado_EnTableAdapter.Fill(this.traficoDataSet.Inteserado_En); 
      // TODO: This line of code loads data into the 'traficoDataSet.Contacto' table. You can move, or remove it, as needed. 
      this.contactoTableAdapter.Fill(this.traficoDataSet.Contacto); 
      // TODO: This line of code loads data into the 'traficoDataSet.Tipo' table. You can move, or remove it, as needed. 
      this.tipoTableAdapter.Fill(this.traficoDataSet.Tipo); 
      // TODO: This line of code loads data into the 'agora_UsuariosDataSet.Usuarios' table. You can move, or remove it, as needed. 
       this.usuariosTableAdapter.Fill(this.agora_UsuariosDataSet.Usuarios); 
      // TODO: This line of code loads data into the 'traficoDataSet.Como' table. You can move, or remove it, as needed. 
      this.comoTableAdapter.Fill(this.traficoDataSet.Como); 
      // TODO: This line of code loads data into the 'traficoDataSet.Trafico' table. You can move, or remove it, as needed. 
      this.traficoTableAdapter.Fill(this.traficoDataSet.Trafico); 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      SqlConnection cn = new  SqlConnection(global::WindowsFormsApplication1.Properties.Settings.Default.TraficoConnectionString); 
      try 
      { 

       string sql = "INSERT INTO Trafico ('Nombre', 'Apedillo', 'Correo', 'Teléfono', 'Como', 'Comercial', 'Tipo', 'Contacto','Inteserado En') Values(" + nombreTextBox.Text + "," + apedilloTextBox.Text + "," + correoTextBox.Text + "," + teléfonoTextBox.Text + "," + comoComboBox.Text + "," + comercialComboBox.Text + "," + tipoComboBox.Text + "," + contactoComboBox.Text + ","+inteserado_EnComboBox.Text+""; 
       SqlCommand exeSql = new SqlCommand(sql, cn); 
       cn.Open(); 
       exeSql.ExecuteNonQuery(); 
       this.traficoTableAdapter.Fill(this.traficoDataSet.Trafico); 
      } 
      catch(Exception ex) 
      { 
       MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
     } 
    } 
} 
+2

これは非常に[SQLインジェクション](https://en.wikipedia.org/wiki/SQL_injection)する傾向があります。 [parameters]または[stored procs]を使用して調べる必要があります。(https://www.owasp.org/index.php/Query_Parameterization_Cheat_Sheet) – BJones

+2

これは、SQLインジェクションだけでなく、多くの理由でこれに同意します。 – JSON

+1

このinsert文にはいくつか問題があります。列名は一重引用符で囲むべきではありません(もちろん、それらの2つを角括弧で囲む必要があります)。あなたは本当にボビーのテーブル(http://bobby-tables.com/)を避けるために値をパラメータ化する必要があります。 SQLインジェクションルートを主張する場合は、文字列リテラルのときに値を一重引用符で囲む必要があります。 –

答えて

0

私はこの問題は、あなたがそう

string sql = "INSERT INTO Trafico ('Nombre', 'Apedillo', 'Correo', 'Teléfono', 'Como', 'Comercial', 'Tipo', 'Contacto','Inteserado En') Values('" + nombreTextBox.Text + "','" + apedilloTextBox.Text + "','" + correoTextBox.Text + "','" + teléfonoTextBox.Text + "','" + comoComboBox.Text + "','" + comercialComboBox.Text + "''," + tipoComboBox.Text + "','" + contactoComboBox.Text + "','" + inteserado_EnComboBox.Text +"')"; 

等は、SQLインジェクションを防ぐために、使用パラメータ上記のような文字列を単一引用符で自分の価値観をラップする必要があると思います。また、複数の行を追加するときに照会を変更する方が簡単です。同じクエリ文字列を使用して、値を入れ替えることができます。あなたはもはやあなたはパラメータにデータ型を定義するための変数の周りに単一引用符を追加する必要はありませんので、

string sql = "INSERT INTO Trafico ('Nombre', 'Apedillo', 'Correo', 'Teléfono', 'Como', 'Comercial', 'Tipo', 'Contacto','Inteserado En') Values(@Nobre, @Apedillo, @ Correo, @Telephono, @Como, @Comercial, @Tipo, @Contacto, @Inteserado_En)"; 


exeSql.Parameters.Add("@Nobre", SqlDataType.Varchar).Value = nombreTextBox.Text; 
exeSql.Parameters.Add("@Apedillo", SqlDataType.Varchar).Value = apedilloTextBox.Text 
//and so on 

のようなパラメータを使用することができます。 sqlコマンドは、そのように定義されているので、文字列のように扱うことを知っています。私はまた、列名 "Inteserado En"のスペースに気づいた。それが正しいことを確認してください。一般的には、列名にスペースを追加しないでください。しかし、それはできます

0

はに文字列のSQLを変更してください。また、すべての列が文字列であることを確認する必要があります。いくつかの数字がある場合は、値の間に "'"文字を省略することができます。

関連する問題