C# Parameters.Add(string, object) 和 Parameters.AddWithValue 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9999751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Parameters.Add(string, object) and Parameters.AddWithValue
提问by phadaphunk
I read the MSDN documentation and examples hereand I know that the correct syntax for a Paramters.Addcall is :
我在这里阅读了 MSDN 文档和示例,我知道Paramters.Add调用的正确语法是:
command.Parameters.Add("@ID", SqlDbType.Int);
command.Parameters["@ID"].Value = customerID;
Where you have to specify the Parameter Name, the SqlDbTypeAND the Value with .Value.
您必须在其中指定参数名称、SqlDbTypeAND 值和.Value.
Now the correct syntax for a Parameters.AddWithValuecall is :
现在Parameters.AddWithValue调用的正确语法是:
command.Parameters.AddWithValue("@demographics", demoXml);
Single line and skip the Typepart.
单行并跳过该Type部分。
My Question is : How is it that when I do it like this,
我的问题是:当我这样做时,怎么办,
command.Parameters.Add("@demographics", demoXml);
// .Add method with .AddWithValue syntax
I don't get any compiling error and even weirder, everything seems to work properly when the code is executed ?
我没有收到任何编译错误,更奇怪的是,执行代码时一切似乎都正常工作?
采纳答案by vcsjones
There is no difference in terms of functionality. In fact, both do this:
在功能方面没有区别。事实上,两者都是这样做的:
return this.Add(new SqlParameter(parameterName, value));
The reason they deprecated the old one in favor of AddWithValueis to add additional clarity, as well as because the second parameter is object, which makes it not immediately obvious to some people which overload of Addwas being called, and they resulted in wildly different behavior.
他们弃用旧的支持的原因AddWithValue是为了增加额外的清晰度,以及因为第二个参数是object,这使得某些人无法立即看出Add正在调用的是哪个重载,并且它们导致了截然不同的行为。
Take a look at this example:
看看这个例子:
SqlCommand command = new SqlCommand();
command.Parameters.Add("@name", 0);
At first glance, it looks like it is calling the Add(string name, object value)overload, but it isn't. It's calling the Add(string name, SqlDbType type)overload! This is because 0 is implicitly convertible to enum types. So these two lines:
乍一看,它看起来像是在调用Add(string name, object value)重载,但实际上不是。它正在调用Add(string name, SqlDbType type)超载!这是因为 0 可以隐式转换为枚举类型。所以这两行:
command.Parameters.Add("@name", 0);
and
和
command.Parameters.Add("@name", 1);
Actually result in two different methods being called. 1is not convertible to an enum implicitly, so it chooses the objectoverload. With 0, it chooses the enum overload.
实际上导致调用两种不同的方法。1不能隐式转换为枚举,因此它选择object重载。使用0,它选择枚举重载。
回答by Khan
Without explicitly providing the type as in command.Parameters.Add("@ID", SqlDbType.Int);, it will try to implicitly convert the input to what it is expecting.
没有像 in 那样显式提供类型command.Parameters.Add("@ID", SqlDbType.Int);,它会尝试将输入隐式转换为它期望的类型。
The downside of this, is that the implicit conversion may not be the most optimal of conversions and may cause a performance hit.
这样做的缺点是隐式转换可能不是最佳的转换,可能会导致性能下降。
There is a discussion about this very topic here: http://forums.asp.net/t/1200255.aspx/1
这里有一个关于这个主题的讨论:http: //forums.asp.net/t/1200255.aspx/1
回答by Dennis Rongo
The difference is the implicit conversion when using AddWithValue. If you know that your executing SQL query (stored procedure) is accepting a value of type int, nvarchar, etc, there's no reason in re-declaring it in your code.
不同之处在于使用 AddWithValue 时的隐式转换。如果您知道正在执行的 SQL 查询(存储过程)正在接受 int、nvarchar 等类型的值,则没有理由在您的代码中重新声明它。
For complex type scenarios (example would be DateTime, float), I'll probably use Add since it's more explicit but AddWithValue for more straight-forward type scenarios (Int to Int).
对于复杂类型的场景(例如 DateTime、float),我可能会使用 Add,因为它更明确,但 AddWithValue 用于更直接的类型场景(Int 到 Int)。
回答by Jeetendra singh negi
When we use CommandObj.Parameter.Add()it takes 2 parameters, the first is procedure parameter and the second is its data type, while .AddWithValue()takes 2 parameters, the first is procedure parameter and the second is the data variable
当我们使用CommandObj.Parameter.Add()它时,它有2个参数,第一个是过程参数,第二个是它的数据类型,而.AddWithValue()有2个参数,第一个是过程参数,第二个是数据变量
CommandObj.Parameter.Add("@ID",SqlDbType.Int).Value=textBox1.Text;
for .AddWithValue
为了 .AddWithValue
CommandObj.Parameter.AddWitheValue("@ID",textBox1.Text);
where IDis the parameter of stored procedure which data type is Int
其中ID是存储过程的参数是哪种数据类型Int
回答by Brijesh Mavani
There is no difference in terms of functionality
功能上没有区别
The addwithvaluemethod takes an object as the value. There is no type data type checking. Potentially, that could lead to error if data type does not match with SQL table. The addmethod requires that you specify the Database type first. This helps to reduce such errors.
该addwithvalue方法将一个对象作为值。没有类型数据类型检查。如果数据类型与 SQL 表不匹配,这可能会导致错误。该add方法要求您首先指定数据库类型。这有助于减少此类错误。
For more detail Please click here
更多详情 请点击这里

