0

Windows フォーム アプリケーションを使用して DB にレコードを追加しようとしています。コードをデバッグすると、例外が発生します: Incorrect syntax near 'value' コードが混乱して申し訳ありません。私は新しいメンバーです。

private void button1_Click(object sender, EventArgs e)
{
     SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial   Catalog=VirtualSalesFair;Integrated Security=True");
     con.Open();
     SqlCommand sc = new SqlCommand("Insert into Empty value('" + textBox1.Text + "'," + textBox2.Text + ",'" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" +textBox9.Text +"',"+textBox10.Text +");", con);
     int o=sc.ExecuteNonQuery();
     MessageBox.Show(o+ ":Record has been inserted");
     con.Close();
}
4

3 に答える 3

3

に変更ValueしますValues

そして、パラメータ化された sqlを使用してください。この種の文字列連結は、SQL インジェクション攻撃に対してオープンです。

using(SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial   Catalog=VirtualSalesFair;Integrated Security=True"))
{
   con.Open();
   SqlCommand sc = new SqlCommand("INSERT INTO Empty VALUES(@VAL1, @VAL2, @VAL3, @VAL4, @VAL5, @VAL6, @VAL7, @VAL8, @VAL9, @VAL10)", con);
   sc.Parameters.AddWithValue("@VAL1", textBox1.Text);
   sc.Parameters.AddWithValue("@VAL2", textBox2.Text);
   sc.Parameters.AddWithValue("@VAL3", textBox3.Text);
   sc.Parameters.AddWithValue("@VAL4", textBox4.Text);
   sc.Parameters.AddWithValue("@VAL5", textBox5.Text);
   sc.Parameters.AddWithValue("@VAL6", textBox6.Text);
   sc.Parameters.AddWithValue("@VAL7", textBox7.Text);
   sc.Parameters.AddWithValue("@VAL8", textBox8.Text);
   sc.Parameters.AddWithValue("@VAL9", textBox9.Text);
   sc.Parameters.AddWithValue("@VAL10", textBox10.Text);

   int o = sc.ExecuteNonQuery();
   MessageBox.Show(o + ":Record has been inserted");
   con.Close();
}
于 2013-10-05T20:16:42.770 に答える
3

値を値に変更します。そう:

insert into empty values("...

これは、行を挿入するための正しい SQL 構文です。

于 2013-10-05T20:18:45.600 に答える
2

VALUESの代わりに使用しValueます。非常に一般的な構文ミスです。

于 2013-10-05T20:19:10.917 に答える