【问题标题】:The parameterized query '(@role nvarchar(5),@count int)StudentProcedure @role,@count OUT' expects the parameter '@count', which was not supplied参数化查询“(@role nvarchar(5),@count int)StudentProcedure @role,@count OUT”需要参数“@count”,但未提供该参数
【发布时间】:2019-06-08 10:32:05
【问题描述】:

//学生管理员

public class Student
    {
        [Key]
        public int StudentId { get; set; }
        public string Name { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Emial { get; set; }
        public string Role { get; set; }
    }

//StudentConroller(这里我叫存储过程)

public class AccountController : Controller
{
    public ApplicationDbClass applicationDbClass;
    public AccountController()
    {
        applicationDbClass = new ApplicationDbClass();
    }
    public ActionResult Login()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Login(Student student)
    {
            var v1 = new SqlParameter();
            v1.ParameterName = "@role";
            v1.SqlDbType = SqlDbType.NVarChar;
            v1.Value = "Admin";

            var v2 = new SqlParameter();
            v2.ParameterName = "@count";
            v2.SqlDbType = SqlDbType.Int;

            try
            {
                var result = applicationDbClass.Students.SqlQuery("StudentProcedure @role,@count OUT", v1, v2).ToArray(); 
            }
            catch(Exception e)
            {
                var m = e.Message;
            }
            return RedirectToAction("Welcome", "Student");
    }
}

//存储过程

CREATE OR ALTER PROCEDURE StudentProcedure
    @role NVARCHAR(30),
    @count INT OUTPUT
AS
BEGIN
    SELECT @count=COUNT(dbo.Students.Role) 
    FROM dbo.Students
    WHERE Role=@role;
END

//DbContext 类

public class ApplicationDbClass : DbContext
    {
        public ApplicationDbClass() : base()
        {
            Database.SetInitializer<ApplicationDbClass>(new DropCreateDatabaseIfModelChanges<ApplicationDbClass>());
        }
        public DbSet<Student> Students { get; set; }
        public DbSet<LogTable> LogTables { get; set; }
    }

// 这里我使用代码优先方法来处理数据库,使用实体框架调用用户创建的存储过程。如果我对存储过程进行一些更改,它将不会直接反映。请给我任何解决方案以反映更改。

【问题讨论】:

  • @count 是一个OUTPUT 参数,因此您必须相应地标记.Direction
  • 添加v2.ParameterDirection = ParameterDirection.Output;。我输入的默认方向。
  • 我得到这个异常“Procedure or function StudentProcedure has too many arguments specified”。

标签: asp.net sql-server entity-framework model-view-controller


【解决方案1】:

你也可以通过这种方式传递参数

SqlConnection cnn = new SqlConnection(cnnString);
SqlCommand cmd = new SqlCommand("StudentProcedure", cnn);
cmd.Connection = cnn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@role", "Admin");
cmd.Parameters.Add("@count", SqlDbType.Int);
cmd.Parameters["@count"].Direction = ParameterDirection.Output;

string Query = cmd.ExecuteNonQuery().ToString();

这里我的目标是展示如何将正常和输出参数传递给程序。

【讨论】:

【解决方案2】:

您的代码中有两个问题。

您应该为output 参数设置默认值。否则C# 代码如果你没有为你的输出参数设置默认值将会抛出一个异常。所以你的存储过程应该是这样的:

ALTER PROCEDURE StudentProcedure
    @role NVARCHAR(30),
    @count INT = NULL OUTPUT
AS
BEGIN
    SELECT @count=COUNT(dbo.Students.Role) 
    FROM dbo.Students
    WHERE Role=@role;   
    SELECT @count;
END
GO

第二个问题是您忘记设置输出参数的Direction

var sqlParameter_Role = new SqlParameter("@role", "Admin");

var sqlParameter_Count = new SqlParameter();
sqlParameter_Count.ParameterName = "@count";
sqlParameter_Count.SqlDbType = SqlDbType.Int;
sqlParameter_Count.Direction = ParameterDirection.Output;

var result = db.Database
    .SqlQuery<ResultForStudentProcedure>("dbo.StudentProcedure @role" 
        , sqlParameter_Role 
        , sqlParameter_Count)
    .ToList();


public class ResultForStudentProcedure
{
    public int Count { get; set; }
}

【讨论】:

  • 不是我,而是 IMO,没有充分的理由为输出参数设置默认值,尤其是在完全忽略传递的参数值(默认或其他)的情况下。也没有明显的理由从过程中生成结果集。
  • @Smor C# 如果您没有为输出参数设置默认值,代码将引发异常。
  • @JaywantNikam 随意提出任何问题,然后您可以将其标记为答案,以简化其他人的未来搜索。另外,请阅读此meta.stackexchange.com/questions/5234/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多