Using SqlParameters
is a recommended method to prevent SQL Injection in your database queries. Where can I find the code/function that internally sanitizes these parameters? I'd like to re-use this function in a custom implementation of mine. I tried to find it using Reflector, but was unsuccessful.
相关问题
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
- Bulk update SQL Server C#
- Should I use static function in c# where many call
It protects against SQL Injection, not XSS, and there is no code or function that sanitizes the parameter data.
The protection is accomplished by transmitting the parameter values to the server separately from the query string, so that the values are never substituted directly into the sql statement.
So instead of sql server running something like this:
It's more as if it ran something like this:
This is faster and much more secure and robust than a function that would have to evaluate the parameter data. Such a function would need to be very complex (read: error prone) to handle things like custom escape characters and future enhancements.
This neatly side steps the whole issue: data is data, code is code, and never the twain shall meet.
Your comment to the other, now deleted, answer:
No, that is not correct. The variable is created directly from a data block, and so no special escaping or encoding is needed.