“名称”附近的语法不正确。必须声明标量变量“@Provider”

时间:2013-06-26 19:12:30

标签: asp.net sql-server

您好我使用下面的代码选择一些数据,我在'名称'附近得到errror不正确的语法。必须声明标量变量“@Provider”。任何想法都有错吗?谢谢!

 cmd = New SqlCommand
        cmd.Connection = Con
        cmd.CommandText = "SELECT NPI, [Entity Type Code], [Provider Last Name (Legal Name)] FROM NPIData WHERE ([Provider Last Name (Legal Name)] LIKE @Provider Last Name (Legal Name) + '%'"
        cmd.Parameters.AddWithValue("@Provider", prefixText)
        Dim customers As List(Of String) = New List(Of String)
        Dim reader As SqlDataReader = cmd.ExecuteReader()

        While reader.Read
            customers.Add(reader("@Provider").ToString)

        End While
        Con.Close()

        Return customers.ToArray

2 个答案:

答案 0 :(得分:2)

我认为你的代码应该是这样的:

cmd.CommandText = "SELECT NPI, [Entity Type Code], [Provider Last Name (Legal Name)] FROM NPIData WHERE ([Provider Last Name (Legal Name)] LIKE @Provider + '%'"
cmd.Parameters.AddWithValue("@Provider", prefixText)
Dim customers As List(Of String) = New List(Of String)
Dim reader As SqlDataReader = cmd.ExecuteReader()

While reader.Read
    customers.Add(reader("[Provider Last Name (Legal Name)]").ToString)

End While

答案 1 :(得分:1)

参数名称/键不能包含空格。

cmd.Parameters.AddWithValue("@Provide", prefixText)

这是所需的语法。