是否有必要增加一个@中的SqlParameter名称前面?有必要、名称、SqlParameter

2023-09-02 01:31:08 作者:幻风

在我们的一个应用程序传递到在存储过程中这种方式的参数

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(看看 的SqlCommand 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 @.