【问题标题】:How to store an int value from stored procedure and pass it to controller如何存储存储过程中的 int 值并将其传递给控制器
【发布时间】:2019-10-08 06:52:25
【问题描述】:

我有如下表格许可证

table Licence
 [ LID(pk) int,
   ProID(FK) int,
   SK string,
   QTY int,
   LIS int,
   LIU int
]

我有“chklin”作为检索 LIU 的存储过程。

procedure [dbo].[chklin]
(@LID int)
as
begin
select LIU from license where LID=@LID
end

现在在我的许可证控制器中,我有以下操作

public ActionResult Chk(int L)
    {
        SLMEntitiesDB dbContext = new SLMEntitiesDB();
        dbContext.chklin(L) // this should check the LIU value, how to pass it 
    to below IF statement

if (A == 0)
    {
        ViewBag.Message = "No more license available";
    }
    else
    {
        return RedirectToAction("create", "users");
    }

【问题讨论】:

  • 欢迎您,您能澄清一下您到底在问什么吗?谢谢
  • 你可以使用函数来检查
  • dbContext.chklin(L) // 这里返回一个int值,如何传递给if语句?

标签: c# sql asp.net-mvc stored-procedures


【解决方案1】:

您可以将过程结果的结果值分配给变量,并在If 语句中检查变量的值。像这样

var A = dbContext.chklin(L)
if(A == 0){
 ViewBag.Message = "No more license available";
} else{
 return RedirectToAction("create", "users");
}

【讨论】:

  • 它不起作用,首先它表明你不能使用 == 因为这是布尔值,当你把它设为一个“=”时,它也会在数字 0 上显示错误,说“不能将 int 隐式转换为 system.data.entity.core.objectresult"
  • 我不确定,但也许正如@Ersin Gülbahar 评论的那样,我应该使用函数而不是存储过程
  • 如果您的存储过程的结果是 int,它肯定可以工作。您可以检查存储过程返回的内容吗?另外,不能用 linq 代替存储过程吗?
  • dbContext.chklin(L) 返回一个 int ,但 var A 显示为 null。
猜你喜欢
  • 1970-01-01
  • 2017-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-26
相关资源
最近更新 更多