问题描述
什么改变 SqlCommand.Parameters.AddWithValue()
确实与查询?
我想到的是:
-
这是由
,
如果参数值是一个字符串或一些东西,必须将其转换为一个字符串,它由包围值
,因此,例如 SELECT *从A其中B = @hello
会给 SELECT *从A其中B ='你好世界
。
If a parameter value is a string or something which must be converted to a string, it surrounds the value by '
, so for example select * from A where B = @hello
will give select * from A where B = 'hello world'
.
如果一个参数值是什么安全之类的整数,它是插在一个查询按原样,不带引号,所以 SELECT *从A其中B = @One
会给 SELECT *从A其中B = 1
。
If a parameter value is something "safe" like an integer, it is inserted in a query as is, without quotes, so select * from A where B = @one
would give select * from A where B = 1
.
有没有我不知道的任何其他更改?
Is there any other changes I'm not aware of?
推荐答案
在ADO.NET的SqlClient驱动程序的不可以做任何替代品!这是一个普遍的误解 - 它避免了更换任何的麻烦
The ADO.NET SqlClient driver will not do any replacements! That's a common misconception - it avoids the trouble of replacing anything.
它的作用是通过您的查询的参数 @参数1 ... @paramN
直接到SQL Server,以及参数名/值对的集合。 SQL Server将执行那些使用 sp_executesql的
存储过程。
What it does is pass your query with the parameters @param1 ... @paramN
straight to SQL Server, along with a collection of parameter name/value pairs. SQL Server then executes those using the sp_executesql
stored proc.
没有替换被做过,有没有串在一起的完整的SQL语句在客户端 - 不一样的。如果这就是ADO.NET的运行时间在做什么,它也将是非常容易受到SQL注入攻击。
No replacements are ever done, there's no "stringing together the complete SQL statement" on the client side - nothing like that. If that's what the ADO.NET runtime were doing, it, too, would be very susceptible to SQL injection attacks.
这篇关于什么SqlCommand.Parameters.AddWithValue真的呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!