使用实体框架从存储过程中获取多个字符串输出

时间:2017-10-12 15:45:45

标签: c# sql entity-framework model-view-controller

以下存储过程显示三个字符串和一个表行结果作为输出。 有没有什么办法可以使用实体框架在mvc视图输出面板上显示所有结果?

我可以在下面的代码中看到第一个字符串结果。但无论如何要获得另外两个选择字符串输出和 表格行结果。

private CustomerEntities db = new CustomerEntities();
        public ActionResult Index()
        {
            var results = db.usp_CustomerData("124544", 1500);
            var abc = results.ToList();
            return View();
        }

ALTER PROCEDURE [dbo].[usp_CustomerData]
@CustomerID varchar(6),
@MinsBack int
AS
BEGIN

    DECLARE @Count int
    SET @Count = (SELECT Count(*)
                    FROM Customer WITH (NOLOCK)
                    WHERE CustomerID = @CustomerID AND                       
                          DATEDIFF(mi, ReceivedAt, GETUTCDATE()) < @MinsBack)
    IF (@Count = 1)
        SELECT 'Ok:  1 message in Customer table'
    ELSE
        SELECT 'ERROR:  Expected 1 message in Customer table, but found ' + CONVERT(varchar(3), @Count) + ' messages.'


    SET @Count = (SELECT Count(*)
                    FROM CustomerDetails WITH (NOLOCK)
                    WHERE CustomerID = @CustomerID AND
                          DATEDIFF(mi, LastUpdatedAt, GETDATE()) < @MinsBack)
    IF (@Count = 1)
        SELECT 'Ok:  1 record in CustomerDetails table'
    ELSE
        SELECT 'ERROR:  Expected 1 record in CustomerDetails table, but found ' + CONVERT(varchar(3), @Count) + ' records.'


    SET @Count = (SELECT Count(*)
                    FROM CustomerProduct WITH (NOLOCK)
                    WHERE CustomerID = @CustomerID AND
                          DATEDIFF(mi, LastUpdatedAt, GETDATE()) < @MinsBack)
    IF (@Count = 1)
        SELECT 'Ok:  1 record in CustomerProduct table'
    ELSE
        SELECT 'ERROR:  Expected 1 record in CustomerProduct table, but found ' + CONVERT(varchar(3), @Count) + ' records.'

    SELECT *FROM Customer where customerID = @CustomerID

END

2 个答案:

答案 0 :(得分:1)

根据建议,您可以在SQL脚本中创建一个临时表,用作临时存储。

CREATE TABLE #Results
(
    Message VARCHAR(512)
)

而不是每个SELECTIF中的直接ELSE,您应该将字符串插入临时表中。 最后,您可以达到目标,让所有插入的字符串返回:

SELECT * FROM #Results

为了吸引客户 - 就像最后一样 - 您应该触发对数据库的新查询。

根据您的情况,您应该考虑按数据上下文查询数据库,而不是按存储过程查询数据库。

答案 1 :(得分:1)

您需要按照link中的建议执行某些操作,但我在下面进行了总结

对于每个结果集,您需要执行reader.NextResult();

Input.GetKeyDown(KeyCode.Escape)