我正在尝试从我的数据库过程中检索数据,但我不确定为什么在应该返回值 1 或 0 时得到返回值 -1。我觉得一切都正确传递并且程序运行良好。但我可能是错的。
我将一个名为“Status”的 int 类型的对象传递到我的数据库过程 db.proc_CsStatus 和 db.proc_GhStatus 中。在调试器期间,虽然我得到“r”的值为-1。
我的控制器:
public ActionResult Index()
{
ViewBag.CsStatus = GhCsStatusProvider.GetCsStatus();
ViewBag.GhStatus = GhCsStatusProvider.GetGhStatus();
return View();
}
我的提供者有这两个功能:
public static int GetGhStatus()
{
using (Entities db = new Entities())
{
System.Data.Objects.ObjectParameter s = new System.Data.Objects.ObjectParameter("Status", typeof(int));
int r = db.proc_CsStatus(120, s);
return r;
}
}
public static int GetCsStatus()
{
using (Entities db = new Entities())
{
System.Data.Objects.ObjectParameter s = new System.Data.Objects.ObjectParameter("Status", typeof(int));
int r = db.proc_CsStatus(120, s);
return r;
}
}
这是我的数据库过程:
对于 proc_GhStatus
USE [DATABASE_GH]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[proc_GhStatus]
-- Add the parameters for the stored procedure here
@TimeLimit Int,
@Status Int OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON
-- Declare variables.
DECLARE @LastUpdate Int
-- Calculate the LastUpdate.
SELECT @LastUpdate = DATEDIFF(second, Timestamp, CURRENT_TIMESTAMP)
FROM Heartbeat
WHERE Id=2
-- Compare it to the TimeLimit.
IF @LastUpdate > @TimeLimit SELECT @Status = 0
ELSE SELECT @Status = 1
END
GO
对于 CsStatus
USE [DATABASE_CS]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[proc_CsStatus]
-- Add the parameters for the stored procedure here
@TimeLimit Int,
@Status Int OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON
-- Declare variables.
DECLARE @LastUpdate Int
-- Calculate the LastUpdate.
SELECT @LastUpdate = DATEDIFF(second, Timestamp, CURRENT_TIMESTAMP)
FROM Heartbeat
WHERE Id=1
-- Compare it to the TimeLimit.
IF @LastUpdate > @TimeLimit SELECT @Status = 0
ELSE SELECT @Status = 1
END
GO