错误:必须声明标量变量

发布于 2024-10-05 23:36:54 字数 1203 浏览 2 评论 0原文

这是我的代码:

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

Which Calls:

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 )

如何修复该行?

谢谢

Here's my code:

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

Which Calls:

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

And when I try to run it, I get this error:

Must declare the scalar variable "@UserId"

On this line:

myAdapter.Fill( ds, tablename )

How can I fix that line?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

固执像三岁 2024-10-12 23:36:54

您从未将参数传递给 SqlDataAdapter。

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

You never passed the parameter to the SqlDataAdapter.

You should change the _select method to use the original SqlCommand, or to copy its parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文