質問編集履歴

1

コードの書き直し

2020/12/08 02:24

投稿

LOL_DESU
LOL_DESU

スコア4

test CHANGED
File without changes
test CHANGED
@@ -72,4 +72,66 @@
72
72
 
73
73
 
74
74
 
75
+ ```C#
76
+
77
+ using System;
78
+
79
+ using Npgsql;
80
+
81
+ namespace C
82
+
83
+ {
84
+
85
+ class Data_Select
86
+
87
+ {
88
+
89
+ public Select(string storage)
90
+
91
+ {
92
+
93
+ //接続文字列
94
+
75
- このコード(少し変えてます)でデータの取得などはできるのですが、例えばstorageの部分にSQLインジェクション対策をするとすればどのような方法があるのか教えていただきたいです。分かりやすい解説サイトなどを張っていただけるだけでもありがたいです。
95
+ string conn_str = "Server=111.111.11.111;Port=5432;User ID=postgres;Database=111;Password=111;Enlist=true";
96
+
97
+ using (NpgsqlConnection conn = new NpgsqlConnection(conn_str))
98
+
99
+ {
100
+
101
+ conn.Open();
102
+
103
+ using (NpgsqlCommand command = new NpgsqlCommand(@" select * FROM syain WHERE id = :value1", conn))
104
+
105
+ {
106
+
107
+ command.Parameters.Add(new NpgsqlParameter("value1", NpgsqlDbType.Integer));
108
+
109
+ command.Parameters[0].Value =int.Parse(storage);
110
+
111
+                       //command.Parameters[1].Value =int.Parse(storage1);
112
+
113
+ //command.Parameters[1].Value =int.Parse(storage2);
114
+
115
+ //ここに一個一個挿入する値を書く?
116
+
117
+ //クエリ実行
118
+
119
+ using (NpgsqlDataReader dr = command.ExecuteReader())
120
+
121
+ {}
122
+
123
+ conn.Close();
124
+
125
+ }
126
+
127
+ }
128
+
129
+ }
130
+
131
+ }
132
+
133
+ ```
134
+
135
+
136
+
137
+ とあるサイトを参考に書いてみて実行も出来たのですが、これでSQLインジェクション対策はできているのでしょうか?