2026年6月12日 星期五

[研究]Mend(WhiteSource)建議了不合理的foreach避免SQL Injection

[研究]Mend(WhiteSource)建議了不合理的foreach避免SQL Injection

2026-06-12

Mend 建議把


SqlCommand command = new SqlCommand(sqlCommandString, conn); conn.Open();

改成


SqlCommand command = new SqlCommand(sqlCommandString, conn); foreach (string id in choiceIDs.Split(',')) { command.Parameters.AddWithValue("@id", id); } conn.Open();

因為同一個 SqlCommand.Parameters 集合裡面,參數名稱必須唯一,添加的名稱都是相同@id,這不合理,會導致 Exception。

(完)