[英]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. 代替在每个
IF
或ELSE
直接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.