简体   繁体   English

多结果存储过程

[英]Multi-result Stored Procedure

i am using Devforce 2012, i have a need to return a multi-result set from a stored procedure to bind to a report (related entities or DataSet). 我在使用Devforce 2012时,需要从存储过程返回一个多结果集以绑定到报表(相关实体或数据集)。 Can this be done in DF 2012? 可以在DF 2012中完成吗?

I have added the stored procedure to the EDM but the result contains only the first table. 我已将存储过程添加到EDM,但结果仅包含第一个表。

Thankyou in advance. 先感谢您。

You can't do this directly with the StoredProcQuery , but you can via an invoked server method. 您不能使用StoredProcQuery直接执行此操作,但是可以通过调用的服务器方法进行。 It's a little tortuous, the server method needs to use the native connection to execute a reader and process the results, and then return a DevForce EntityCacheState . 这有点曲折,服务器方法需要使用本机连接来执行读取器并处理结果,然后返回DevForce EntityCacheState You then load the EntityCacheState into your EntityManager . 然后,您将EntityCacheState加载到EntityManager It would look something like this: 它看起来像这样:

-- on the client -- -在客户端上-

  var em = new NorthwindEntities();
  var ecs = em.InvokeServerMethod("Mynamespace.MyClass, MyExe", "TestProc") as EntityCacheState;
  em.CacheStateManager.RestoreCacheState(ecs);

On the server, it's a bit tricky to get the connection - you can easily create an EF ObjectContext from the connection string, and then from that you create a DbContext , and then open and use the connection. 在服务器上,获取连接有点棘手-您可以轻松地从连接字符串创建EF ObjectContext ,然后从中创建DbContext ,然后打开并使用连接。 For example: 例如:

-- on the server -在服务器上

[AllowRpc]
public static EntityCacheState TestProc(IPrincipal principal, EntityManager em, params Object[] args) {
  var cs = ((ClientEdmKey)em.DataSourceResolver.GetDataSourceKey("NorthwindEntities")).ConnectionString;
  using (var ctx = new ObjectContext(cs))
  using (var db = new DbContext(ctx, true)) {
    var cn = db.Database.Connection;
    cn.Open();
    var cmd = cn.CreateCommand();
    cmd.CommandText = "[dbo].[mystoredproc]";

    // Let's say this proc returns Customers and Suppliers
    // Read each result set and attach entities to the EM

    var reader = cmd.ExecuteReader();
    var customers = ctx.Translate<Customer>(reader);
    em.AttachEntities(customers);

    reader.NextResult();
    var suppliers = ctx.Translate<Supplier>(reader);
    em.AttachEntities(suppliers);

    // Return the EntityCacheState to the client
    return em.CacheStateManager.GetCacheState();
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM