考慮すべき点は次のとおりです。
using (SqlConnection c = new SqlConnection(connString))
{
c.Open();
using (SqlCommand cmd = new SqlCommand("INSERT INTO [Table] ([Item], [Des1],[Des2], [Prodline], [ANR], [STime]) VALUES(@Item,@Des1,'0',@ProdLine,@ANR,@STime)", c))
{
cmd.Parameters.AddWithValue("@Item", txtText1.Text);
cmd.Parameters.AddWithValue("@Des1", txtText2.Text);
cmd.Parameters.AddWithValue("@ProdLine", txText3.Text);
cmd.Parameters.AddWithValue("@ANR", txtText4.Text);
cmd.Parameters.AddWithValue("@STime", txtText5.Text);
cmd.ExecuteNonQuery();
}
}
構築中の SQL ステートメントをサーバーに送信する場合、次のようになります。
INSERT INTO [Table] ([Item], [Des1],[Des2], [Prodline], [ANR], [STime])
VALUES(Item Name,Here is my description.,'0',Prod line,ANR value,some time)
それが失敗するのは明らかだ。値を一重引用符で囲んでさえいません。さて、それらのすべてがキャラクターではないかもしれませんが、それはわかりますが、あなたは私の主張を理解しています.
しかし、怪我に侮辱を加えるために、あなたが書いた方法でSQLインジェクションに対して広く開かれています。パラメータ化されたアプローチではそうではありません。