繁体   English   中英

我如何解决此过程或函数“SelectFollow”需要未提供的参数“@userid”。

[英]How do i resolve this procedure or function 'SelectFollow' expects parameter '@userid', which was not supplied.'?

我正在尝试从参数中获取当前用户 ID,并使用存储过程检查它是否存在于数据库表中

public PartialViewResult AllStates(string state, string userid)
{

    SqlCommand cmd = new SqlCommand();
    Following following = new Following();
    cmd.Parameters.Add("@userid", SqlDbType.BigInt).Value = userid;
    List<Farmer> model = db.Farmer.Where(u => u.FarmState == state).Take(3).ToList();
    var modelfollow = db.Following.SqlQuery("[dbo].[SelectFollow]").ToList();
    ViewBag.folo = modelfollow;
    return PartialView("_UsersList", model );
}

这是我的存储过程

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE SelectFollow 
    @userid INT
AS
BEGIN
DECLARE @HasFollowed BIT
    IF EXISTS(SELECT * FROM dbo.Followings Where UserId = @userid)
BEGIN
SET @HasFollowed = 1
END
ELSE
BEGIN
SET @HasFollowed = 0
END
END
GO

有什么我做错了吗?

问题出在这一行,其中不包含任何存储过程 (SP) 参数,而 SP 需要一个参数,因此引发了标题中提到的异常:

var modelfollow = db.Following.SqlQuery("[dbo].[SelectFollow]").ToList();

此外,您正在向包含空查询字符串的SqlCommand实例添加参数(之后您不会执行它):

SqlCommand cmd = new SqlCommand();
cmd.Parameters.Add("@userid", SqlDbType.BigInt).Value = userid;

由于您使用的是DbSet.SqlQuery()方法,您可以通过声明SqlParameter实例将查询参数作为该方法的第二个参数传递:

public PartialViewResult AllStates(string state, string userid)
{
    // define stored procedure parameter here
    var parameter = new SqlParameter("@userid", SqlDbType.BigInt);
    parameter.Value = userid;

    List<Farmer> model = db.Farmer.Where(u => u.FarmState == state).Take(3).ToList();

    // use stored procedure parameter declared above
    var modelfollow = db.Following.SqlQuery("[dbo].[SelectFollow] @userid", parameter).ToList();
    ViewBag.folo = modelfollow;
    return PartialView("_UsersList", model);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM