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
即代码中传入的参数名是 searchText
,存储过程中的参数名是 @searchText
.
i.e. 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.
推荐答案
根据 到文档,名称必须以 @
开头:
According to the documentation, the name must start with an @
:
ParameterName 以@paramname 的形式指定.
The ParameterName is specified in the form @paramname.
根据源代码(看看SqlCommand
和 参考源中的SqlParameter.ParameterNameFixed
),如果需要,会自动添加一个@
.
所以是的,它可以工作,但它是一个未记录的功能.最佳实践建议您不要依赖此方法,而是手动使用 @
作为参数名称的前缀.
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屋!