错误:必须声明标量变量

时间:2010-12-06 18:31:00

标签: sql vb.net dataset prepared-statement

这是我的代码:

Public Function selectReturnsByUserId( limit As Integer, userid As String ) As DataSet 



    Dim sql As String = " SELECT TOP " & limit & " pr.ProductId, p.Title, p.Barcode, pr.ScanDate, pr.UserId, pr.ReturnStatus" & _
                        " FROM " & tablename & " pr " & _
                        "   INNER JOIN " & StaticValues.TABLENAME_PRODUCT & " p ON pr.ProductId = p.ProductId" & _
                        " WHERE pr.UserId = @UserId"

    Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand( sql )

    cmd.Parameters.AddWithValue( "@UserId", userid ) 

    Return _select( cmd )

End Function

哪个电话:

Protected Function _select(ByVal cmd As SqlClient.SqlCommand) As DataSet

    Dim ds As New DataSet
    Dim myAdapter As New System.Data.SqlClient.SqlDataAdapter(cmd.CommandText, DBConnection.getInstance().getConnection().ConnectionString)
    myAdapter.Fill( ds, tablename )

    Return ds

End Function

当我尝试运行它时,我收到此错误:

必须声明标量变量“@UserId”

在这一行:

myAdapter.Fill( ds, tablename )

如何修复该行?

由于

1 个答案:

答案 0 :(得分:2)

您从未将参数传递给SqlDataAdapter。

您应该更改_select方法以使用原始SqlCommand,或复制其参数。