使用ObjectContext从存储过程中检索多个结果集

时间:2015-12-23 06:34:36

标签: c# sql-server stored-procedures objectcontext

我有一个存储过程:

CREATE PROCEDURE uspGetData
   ...Parameter
AS
   SELECT 'hello' AS 'HelloColumn'
GO

然后我使用ObjectContext来执行存储过程

((IOjbectContextAdapter)this).ObjectContext.ExecuteFunction<HelloClass>("uspGetData", .... Parameter);

HelloClass只是一个数据类

public class HelloClass
{
    public string HelloColumn { ... }
}

我得到了我需要的完美结果

但现在我需要在存储过程中添加另一个SELECT,如下所示

CREATE PROCEDURE uspGetData
   ...Parameter
AS
   SELECT 'hello' AS 'HelloColumn'
   SELECT 'byebye' AS 'ByeColumn'
GO

当然,在真实环境中更复杂,不仅仅是SELECT something

现在我无法得到我需要的结果,

我尝试在HelloClass

上做基类
public class HelloClass : ByeClass
{ ... }

我也尝试在ObjectContext命令

中插入2个对象集
((IOjbectContextAdapter)this).ObjectContext.ExecuteFunction<HelloClass,ByeClass>...

无论如何都要从存储过程中设置两个结果吗?

1 个答案:

答案 0 :(得分:0)

是的,有办法。

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);
}

了解更多信息请查看: https://msdn.microsoft.com/en-us/data/jj691402.aspx