简体   繁体   English

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

[英]get multiple string outputs from stored procedure using entity framework

The following stored procedure displays three strings and a table row result as output. 以下存储过程显示三个字符串和一个表行结果作为输出。 Is there any way we can display all the results on a mvc view output panel using entity framework? 有什么方法可以使用实体框架在mvc视图输出面板上显示所有结果?

I could see the first string result in the code below. 我可以在下面的代码中看到第一个字符串的结果。 But is there anyway to get the other two select string outputs and the table row result. 但是无论如何,都可以获得其他两个选择字符串输出和表行结果。

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

As suggestion you could create a temporary table in your SQL script which will be used as temporary store. 作为建议,您可以在SQL脚本中创建一个临时表,该表将用作临时存储。

CREATE TABLE #Results
(
    Message VARCHAR(512)
)

Instead of a direct SELECT in each IF or ELSE you should insert the string into the temp table. 代替在每个IFELSE直接SELECT ,您应该将字符串插入到临时表中。 At the end you could reach your goal to get all inserted strings to return them by: 最后,您可以达到目标,通过以下方式获取所有插入的字符串以返回它们:

SELECT * FROM #Results

To get customers - like you do at the end - you should trigger a new query to database. 为了获得客户(就像最后一样),您应该触发对数据库的新查询。

Depending on your case you should consider to querying the database by data context instead of querying the database by store procedures. 根据您的情况,您应该考虑通过数据上下文查询数据库,而不是通过存储过程查询数据库。

You need to do something as suggest in this link but I summarized below 您需要执行此链接中建议的操作,但我在下面总结了

For each results set you will need to do a reader.NextResult(); 对于每个结果集,您需要做一个reader.NextResult();。

var someReturnObject = new ResultObject();

using (var context = new LinqPadDbContext(@"Server=localhost\SQLEXPRESS;Database=StackOverflow;Trusted_Connection=True;"))
{
    var cmd = context.Database.Connection.CreateCommand();
    cmd.CommandText = "[dbo].[GetSomeData]";

    try
    {           
        context.Database.Connection.Open();

        var reader = cmd.ExecuteReader();

        var result1 = ((IObjectContextAdapter)context).ObjectContext.Translate<string>(reader);

        someResultObject.Text1 = result1.First();

        //for each extra result, start here
        reader.NextResult();

        var users = ((IObjectContextAdapter)context).ObjectContext.Translate<User>(reader);

        someResultObject.Users = users.Select(x => x);
        //stop here
    }
    finally
    {
        context.Database.Connection.Close();
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架-使用存储过程获取多个表中的记录 - Entity Framework - get records in multiple tables using stored procedure 使用存储过程实体框架从表中获取列表 - get list from table using stored procedure entity framework 如何使用实体框架执行存储过程并从存储过程获取输出 - How to execute stored procedure and get output from the stored procedure using Entity Framework 在实体框架中使用存储过程 - using stored procedure in entity framework 使用 Entity Framework 计算列通过存储过程获取多个结果集 - Get multiple result set with stored procedure using Entity Framework calculated column 实体框架4-如何从存储过程中读取多个记录集? - Entity Framework 4 - How to Read Multiple Record Sets From A Stored Procedure? 使用 ExecuteSqlCommand 从存储过程获取返回值(使用实体框架) - Get return value from stored procedure using ExecuteSqlCommand (using Entity Framework) 如何使用 FromSqlRaw Entity Framework Core 3.1 从存储过程返回多个 SELECT 集 - How to return multiple SELECT sets from a stored procedure using FromSqlRaw Entity Framework Core 3.1 如何使用存储过程在Entity Framework中获取数据集合 - How to get a collection of data in Entity Framework using stored procedure 实体框架从存储过程获取选择命令对象 - Entity Framework get a select command object from a stored procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM