Thursday, 19 September 2013

c# return value of dictionary from stored procedure

c# return value of dictionary from stored procedure

I have a function that grabs the output from a stored procedure. However,
I bit of JSON and all it's outputs being displayed are 'true'. I'm not
sure if I'm returning or placing the values correctly into my dictionary.
Here is my function
public static Dictionary<string,int> GetGhCsStatus()
{
using (Entities db = new Entities())
{
System.Data.Objects.ObjectParameter CsOut = new
System.Data.Objects.ObjectParameter("CsStatus", typeof(int));
System.Data.Objects.ObjectParameter GhOut = new
System.Data.Objects.ObjectParameter("GhStatus", typeof(int));
System.Data.Objects.ObjectParameter CsRunValue = new
System.Data.Objects.ObjectParameter("CSRunningValue",
typeof(int));
System.Data.Objects.ObjectParameter GhRunValue = new
System.Data.Objects.ObjectParameter("GHRunningValue",
typeof(int));
int r = db.proc_GhCsStatus(CsOut, GhOut, CsRunValue, GhRunValue);
Dictionary<string, int> Status = new Dictionary<string, int>();
Status.Add("CsStatus", (int)CsOut.Value);
Status.Add("GhStatus", (int)GhOut.Value);
Status.Add("CheckIfCsIsRunning", (int)CsRunValue.Value);
Status.Add("CheckIfGhIsRunning", (int)GhRunValue.Value);
return Status;
}
}
my stored procedure:
USE [DATABASE]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[proc_GhCsStatus]
-- Add the parameters for the stored procedure here
-- @TimeLimit Int, Do not need this paramter as it is grabbed from
Messenger_Settings table
@CsStatus Int OUTPUT,
@GhStatus Int OUTPUT,
@CSRunningValue Int OUTPUT,
@GHRunningValue Int OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Declare variables.
DECLARE @CsLastUpdate Int;
DECLARE @GhLastUpdate Int;
DECLARE @CsTimeout Int; -- Used to set Timeout
DECLARE @GhTimeout Int; -- Used to set Timeout
DECLARE @CheckGHRunningValue varchar(10);
DECLARE @CheckCSRunningValue varchar(10);
-- Calculate the LastUpdate.
SELECT @CsLastUpdate = DATEDIFF(second, Timestamp, CURRENT_TIMESTAMP)
FROM Heartbeat
WHERE Id=1
-- Calculate the LastUpdate.
SELECT @GhLastUpdate = DATEDIFF(second, Timestamp, CURRENT_TIMESTAMP)
FROM Heartbeat
WHERE Id=2
-- Grab CSTimeout from Messenger_Settings table
SELECT @CsTimeout = SettingValue
FROM Messenger_Settings
WHERE SettingName = 'CSTimeout'
-- Grab GHTimeout from Messenger_Settings table
SELECT @GhTimeout = SettingValue
FROM Messenger_Settings
WHERE SettingName = 'GHTimeout'
-- Get CS flag.
SELECT @CheckCSRunningValue = SettingValue
FROM Messenger_Settings
WHERE SettingName = 'CheckIfCSIsRunning'
-- Get GH flag.
SELECT @CheckGHRunningValue = SettingValue
FROM Messenger_Settings
WHERE SettingName = 'CheckIfGHIsRunning'
-- Compare it to the TimeLimit.
IF @CsLastUpdate > @CsTimeout SELECT @CsStatus = 0;
ELSE SELECT @CsStatus = 1;
-- Compare it to the TimeLimit.
IF @GhLastUpdate > @GhTimeout SELECT @GhStatus = 0;
ELSE SELECT @GhStatus = 1;
-- Convert true and false to 1 and 0
IF @CheckCSRunningValue = 'false' AND @CheckGHRunningValue = 'false'
SELECT @GHRunningValue = 0, @CSRunningValue = 0
ELSE IF @CheckCSRunningValue = 'false' AND @CheckGHRunningValue =
'true'
SELECT @GHRunningValue = 0, @CSRunningValue = 1
ELSE IF @CheckCSRunningValue = 'true' AND @CheckGHRunningValue =
'false'
SELECT @GHRunningValue = 1, @CSRunningValue = 0
ELSE IF @CheckCSRunningValue = 'true' AND @CheckGHRunningValue =
'true'
SELECT @GHRunningValue = 1, @CSRunningValue = 1
END
GO

No comments:

Post a Comment