简体   繁体   English

数据库级别的参数化查询会发生什么

[英]What happens to a parameterized query at database level

I am working on few SQL injection bugs flagged by one of vulnerability scanner and was looking at some of other implementations in application where parameterized queries been used for DB interactions .我正在研究由漏洞扫描器之一标记的少数 SQL 注入错误,并且正在研究应用程序中的一些其他实现,其中参数化查询用于数据库交互。

I observed in the profiler that all the parameterized queries are actually calling sp_executesql procedure .So,我在探查器中观察到所有参数化查询实际上都在调用sp_executesql过程。所以,

1) Do all parameterized query implementations with any library are actually just calling this stored procedure ? 1) 是否所有带有任何库的参数化查询实现实际上只是调用这个存储过程?

2)If no then, is a parameterized query finally converted to just a normal string query and gets executed? 2)如果没有,那么参数化查询是否最终转换为普通的字符串查询并被执行?

I cannot answer 1).我无法回答 1)。 But you can pass the parameter names as well as a varying number of parameters to sp_executesql (Transact-SQL) .但是您可以将参数名称以及不同数量的参数传递给sp_executesql (Transact-SQL) So sp_executesql is not a limiting factor here.所以sp_executesql不是这里的限制因素。

  1. The database does not create a concatenated string for parametrized queries.该数据库创建参数化查询一个连接字符串。 It compiles the SQL command string as is, ie with the parameter names and produces an executable query.它按原样编译 SQL 命令字符串,即使用参数名称并生成可执行查询。 You can think of it as a method.您可以将其视为一种方法。 The parameter values are then passed to this "method" as real parameters.然后将参数值作为实际参数传递给这个“方法”。

Besides defeating SQL injection, this has the advantage that the database can cache the compiled query and reuse it the next time the same SQL command is executed, even with different parameter values.除了打败 SQL 注入之外,这还有一个优点,即数据库可以缓存已编译的查询,并在下次执行相同的 SQL 命令时重用它,即使使用不同的参数值。

Yet another advantage is that you don't have to care about the right representation of literals.另一个优点是您不必关心文字的正确表示。 This is especially valuable for date/time literals (which tends to be quite complicated because formats are culture specific and can vary otherwise).这对于日期/时间文字特别有价值(这往往非常复杂,因为格式是特定于文化的,否则可能会有所不同)。 You don't need to care about escaping quotes in strings.您不需要关心在字符串中转义引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 参数化查询…使用C#在数据库中意外 - parameterized query… unexpected in database using C# 什么是“未提供参数化查询...。”错误? - What is “The parameterized query … which was not supplied.” error? 如果查询超时,会发生什么情况? - What Happens To a Query If It Times Out? 参数化查询与% - Parameterized query with % 当我的LINQ查询获取数据库记录为Enumerable并在foreach循环中访问记录时会发生什么? - What happens when my LINQ query gets database records as Enumerable and accesses records in a foreach loop? C#实体框架-用子查询填充列表时,数据库中会发生什么? - C# Entity Framework - What happens in the database when you fill a list with a sub query? C#数据库插入:参数化查询需要未提供的参数 - C# Database Insert: The parameterized query expects the parameter, which was not supplied 如何在.mdf数据库上执行参数化的选择查询并显示列值? - How to execute parameterized select query on .mdf database and display a column value? 每5次迭代保存到数据库中,但是如果迭代次数少,会发生什么呢? - Save to database every 5 iterations but what happens if there are less? 如何通过参数化查询在数据库中插入空值 - How to insert null value in Database through parameterized query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM