Handle multiple result from a stored procedure with SqlQuery

前端 未结 2 408
孤城傲影
孤城傲影 2020-12-30 10:42

I have a stored procedure which returns a multiple set of results (two tables). I call the stored procedure like this:

var result = context.Database.SqlQuery         


        
2条回答
  •  我在风中等你
    2020-12-30 11:22

    I think following code could help you.

     MultiResultDomain domainEntity = new MultiResultDomain();
         var command = _DatabaseContext.Database.Connection.CreateCommand();
         command.CommandText = "[dbo].[SPR_GETMultipleResultSP]";
         command.CommandType = CommandType.StoredProcedure;
         try
         {
             _DatabaseContext.Database.Connection.Open();
             var reader = command.ExecuteReader();
    
         List _listOfCustomer =
         ((IObjectContextAdapter)_DatabaseContext).ObjectContext.Translate
         (reader).ToList();
             reader.NextResult();
             List _listOfProduct =
                 ((IObjectContextAdapter)_DatabaseContext).ObjectContext.Translate
         (reader).ToList();
    
             foreach (var cust in _listOfCustomer)
             {
                 Console.WriteLine("Name: Mr.{0} And Country: {1}", cust.FirstName,
                 cust.Country);
             }
    
             foreach (var product in _listOfProduct)
             {
                 Console.WriteLine("ProductName: {0} And Package: {1}",
                 product.ProductName, product.Package);
             }
    
             domainEntity.Customer = _listOfCustomer;
             domainEntity.Product = _listOfProduct;
             return domainEntity;
    

    Ref: Click here

提交回复
热议问题