是否有必要增加一个@中的SqlParameter名称前面? [英] Is it necessary to add a @ in front of an SqlParameter name?
问题描述
在我们的一个应用程序传递到在存储过程中这种方式的参数
In one of our application the parameters passed to a stored procedure in this way
Dim parm As New SqlParameter("searchText", SqlDbType.VarChar)
parm.Direction = ParameterDirection.Input
parm.Size = 50
parm.Value="test"
cmd.Parameters.Add(parm)
和程序包含一个参数作为 @searchText
and the procedure contains a parameter as @searchText
,即从code传递的参数名称是 searchText
和存储过程中的 @searchText
。
ie the parameter name passed from the code is searchText
and that in the stored procedure is @searchText
.
但它工作正常,我总是得到所需的结果。
But it is working properly, i am always getting the required results.
所以我的问题是,像这样没有必要的参数之前指定的 @ ?是否会追加 @ ,任何人都可以请给一个答案这一点。
So my question is like so there is no need to specify @ before the parameter? Whether it will append @, can anyone please give an answer for this.
推荐答案
据的到文档,名称必须以 @
:
该参数名称中指定的形式@paramname。
The ParameterName is specified in the form @paramname.
据消息人士透露code(看看<一href="http://referencesource.microsoft.com/System.Data/data/System/Data/SqlClient/SqlCommand.cs.html"相对=nofollow> 的SqlCommand
和<一href="http://referencesource.microsoft.com/System.Data/data/System/Data/SqlClient/SqlParameter.cs.html#e0b0ddfdcb0fca98"相对=nofollow> SqlParameter.ParameterNameFixed
参考源),一个 @
自动添加如果需要
According to the source code (have a look at SqlCommand
and SqlParameter.ParameterNameFixed
in the reference source), an @
is added automatically, if needed.
所以,是的,它的工作原理,但它是一个未公开的特性。最佳实践建议您不要依赖此并手动preFIX您的参数名称与 @
。
So yes, it works, but it's an undocumented feature. Best practice recommends that you do not rely on this and manually prefix your parameter name with an @
.
这篇关于是否有必要增加一个@中的SqlParameter名称前面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!