teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コードの書き直し

2020/12/08 02:24

投稿

LOL_DESU
LOL_DESU

スコア4

title CHANGED
File without changes
body CHANGED
@@ -35,4 +35,35 @@
35
35
 
36
36
  ### 試したこと
37
37
 
38
+ ```C#
39
+ using System;
40
+ using Npgsql;
41
+ namespace C
42
+ {
43
+ class Data_Select
44
+ {
45
+ public Select(string storage)
46
+ {
47
+ //接続文字列
38
- このコード(少し変えてます)でデータの取得などはできるのですが、例えばstorageの部分にSQLインジェクション対策をするとすればどのような方法があるのか教えていただきたいです。分かりやすい解説サイトなどを張っていただけるだけでもありがたいです。
48
+ string conn_str = "Server=111.111.11.111;Port=5432;User ID=postgres;Database=111;Password=111;Enlist=true";
49
+ using (NpgsqlConnection conn = new NpgsqlConnection(conn_str))
50
+ {
51
+ conn.Open();
52
+ using (NpgsqlCommand command = new NpgsqlCommand(@" select * FROM syain WHERE id = :value1", conn))
53
+ {
54
+ command.Parameters.Add(new NpgsqlParameter("value1", NpgsqlDbType.Integer));
55
+ command.Parameters[0].Value =int.Parse(storage);
56
+                       //command.Parameters[1].Value =int.Parse(storage1);
57
+ //command.Parameters[1].Value =int.Parse(storage2);
58
+ //ここに一個一個挿入する値を書く?
59
+ //クエリ実行
60
+ using (NpgsqlDataReader dr = command.ExecuteReader())
61
+ {}
62
+ conn.Close();
63
+ }
64
+ }
65
+ }
66
+ }
67
+ ```
68
+
69
+ とあるサイトを参考に書いてみて実行も出来たのですが、これでSQLインジェクション対策はできているのでしょうか?