不,你在安全方面不。即使dataChoosed
是一個無辜的整數值,壞男孩可以用,比如說,負值格式傷害你:
// It's good old "-1", with a bit strange format
// (let use "delete from table commit;" as an injection)
string dataChoosed = "1'; delete from table commit; --1";
// A little hack: let "-" sign be...
CultureInfo hacked = new CultureInfo("en-US");
hacked.NumberFormat.NegativeSign = "1'; delete from table commit; --";
Thread.CurrentThread.CurrentCulture = hacked;
if (dataChoosed != "randomValue")
{
int v;
// since "1'; delete from table commit; --1" is of correct fotmat it will be parsed
if (int.TryParse(dataChoosed, out v))
sCondition = " WHERE RandomField = '" + dataChoosed + "' ";
}
cd.CommandText = "SELECT xData FROM table " + sCondition + "GROUP BY xxx";
而且,榮辱與共!我的桌子在哪兒?該命令文本將
SELECT xData FROM table = '1'; delete from table commit; --1'GROUP BY xxx
這是efficently 查詢:
SELECT xData FROM table = '1'; -- the innocent one
delete from table commit; -- an arbitrary query from the attacker
(我已經刪除註釋掉--1'GROUP BY xxx
片段)
請使用參數,不要誘惑我們。請注意,您不想更改代碼:您只需在Windows中更改區域設置。
您應該使用參數。將SQL與參數連接起來沒有任何問題。 – SLaks
請務必使用參數,另請參見[最佳實踐 - 執行Sql語句](http://stackoverflow.com/documentation/.net/3589/ado-net/14261/best-practices-executing-sql-statements)以獲取更多信息和希望。 – Igor
「選擇」的過去分詞是「選擇」,而不是「選擇」。如果變量名稱在其中有正確的拼寫,它會讓其他人更容易查看你的代碼:) –