C# 使用ObjectContext从存储过程中检索多个结果集
我有一个存储过程:C# 使用ObjectContext从存储过程中检索多个结果集,c#,sql-server,stored-procedures,objectcontext,C#,Sql Server,Stored Procedures,Objectcontext,我有一个存储过程: CREATE PROCEDURE uspGetData ...Parameter AS SELECT 'hello' AS 'HelloColumn' GO 然后我使用ObjectContext执行存储过程 ((IOjbectContextAdapter)this).ObjectContext.ExecuteFunction<HelloClass>("uspGetData", .... Parameter); 我得到了我需要的完美结果 但是现在我需
CREATE PROCEDURE uspGetData
...Parameter
AS
SELECT 'hello' AS 'HelloColumn'
GO
然后我使用ObjectContext
执行存储过程
((IOjbectContextAdapter)this).ObjectContext.ExecuteFunction<HelloClass>("uspGetData", .... Parameter);
我得到了我需要的完美结果
但是现在我需要像这样在存储过程中添加另一个SELECT
CREATE PROCEDURE uspGetData
...Parameter
AS
SELECT 'hello' AS 'HelloColumn'
SELECT 'byebye' AS 'ByeColumn'
GO
当然,在真实的环境中更为复杂,而且不仅仅是做选择某件事
现在我无法得到我需要的结果
我试图在HelloClass
public class HelloClass : ByeClass
{ ... }
我还尝试在ObjectContext
命令中插入2个对象集
((IOjbectContextAdapter)this).ObjectContext.ExecuteFunction<HelloClass,ByeClass>...
((IOjbectContextAdapter)this).ObjectContext.ExecuteFunction。。。
是否仍要从存储过程中同时获取两个结果集?是的,有一种方法
using (var db = new YourContext())
{
var cmd = db.Database.Connection.CreateCommand();
cmd.CommandText = "YourStoredProcedure";
db.Database.Connection.Open();
var reader = cmd.ExecuteReader();
ObjectResult<HelloClass> table1 = ((IObjectContextAdapter)db).ObjectContext.Translate<HelloClass>(reader, "HelloClass", MergeOption.AppendOnly);
reader.NextResult();
ObjectResult<HelloClass2> table2 = ((IObjectContextAdapter)db).ObjectContext.Translate<HelloClass2>(reader, "HelloClass2", MergeOption.AppendOnly);
}
使用(var db=new YourContext())
{
var cmd=db.Database.Connection.CreateCommand();
cmd.CommandText=“YourStoredProcess”;
db.Database.Connection.Open();
var reader=cmd.ExecuteReader();
ObjectResult table1=((IOObjectContextAdapter)db.ObjectContext.Translate(读取器,“HelloClass”,MergeOption.AppendOnly);
reader.NextResult();
ObjectResult table2=((IOObjectContextAdapter)db.ObjectContext.Translate(读取器,“HelloClass2”,MergeOption.AppendOnly);
}
有关更多信息,请查看以下内容:
不,据我所知,EF不支持具有多个结果集的存储过程-您要么需要有多个单独的存储过程,每个存储过程返回一个结果集,要么需要使用“原始”ADO.NET来处理这些结果
using (var db = new YourContext())
{
var cmd = db.Database.Connection.CreateCommand();
cmd.CommandText = "YourStoredProcedure";
db.Database.Connection.Open();
var reader = cmd.ExecuteReader();
ObjectResult<HelloClass> table1 = ((IObjectContextAdapter)db).ObjectContext.Translate<HelloClass>(reader, "HelloClass", MergeOption.AppendOnly);
reader.NextResult();
ObjectResult<HelloClass2> table2 = ((IObjectContextAdapter)db).ObjectContext.Translate<HelloClass2>(reader, "HelloClass2", MergeOption.AppendOnly);
}