错误:必须声明标量变量
这是我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您从未将参数传递给 SqlDataAdapter。
您应该更改
_select
方法以使用原始SqlCommand
,或复制其参数。You never passed the parameter to the SqlDataAdapter.
You should change the
_select
method to use the originalSqlCommand
, or to copy its parameters.