แอบเนียนบทความเก่าหน่อย ^^
http://www.nextproject.net/contents/default.aspx?00042 
แต่บางครั้งเอาไม่อยุ่ครับโดยเฉพาะข้อมูลที่เป็น String มันมีอีกเทคนิคนึงที่ส่งตรงผ่าน URL ที่เข้ามาได้ แบบไม่มี ' เคยโดนจังๆมาครั้งนึง แบบเทรกสคริป มาไกลจากรัสเซีย หาอยู่หลายวัน ดู Log ถึงรู้ ไม่ขอเปิดเผยเพราะเป็นดาบ 2 คม
เอาเป็นว่า ถ้าเป็นไปได้ให้ Fix command บางตัวของข้อมูลที่รับเข้ามา ก่อนส่งต่อรวม Query String ใช้ Function replace ก็ได้เอา พวก "'", "--", ";--", "/*", "*/", "nchar", "varchar", "nvarchar", "ntext" ออก หรือค่ารับมาเป็นตัวเลข ก็ครอบ function พวก cint() ก่อน
ตย. asp.net ประมาณนี้
Public Shared Function fix_sql(ByVal data As String) As String
Dim blackList As String() = {"'", "--", ";--", "/*", "*/", "nchar", "varchar", "nvarchar", "ntext"}
Dim whiteList As String() = {"''", "~", "~~", "/", "/", "nchar[]", "varchar[]", "nvarchar[]", "ntext[]"}
For i As Integer = 0 To blackList.Length - 1
If (data.IndexOf(blackList(i), StringComparison.OrdinalIgnoreCase) >= 0) Then
data = Regex.Replace(data, blackList(i), whiteList(i), RegexOptions.IgnoreCase)
End If
Next
Return (Trim(data))
End Function
หรือถ้ากรองเต็มก็ใช้
'Dim blackList As String() = {"'", "--", ";--", "/*", "*/", "@@", "nchar", "varchar", "nvarchar", "ntext", "alter", "cast", "create", "cursor", "declare", "delete", "drop", "exec", "execute", "fetch", "insert", "kill", "select", "sys", "sysobjects", "syscolumns", "table"}
'Dim whiteList As String() = {"''", "~~", ":~~", "|", "|", "[@@]", "nchar[]", "varchar[]", "nvarchar[]", "ntext[]", "[alter]", "cast[]", "[create]", "[cursor]", "[declare]", "[delete]", "[drop]", "[exec]", "[execute]", "[fetch]", "[insert]", "[kill]", "[select]", "[sys]", "[sysobjects]", "[syscolumns]", "
"}
แต่แบบนี้บางอย่างมัน replace มั่วไปหมด |