zoukankan      html  css  js  c++  java
  • MultipleResultSets stored procedure entity framework asp.net

    ALTER PROCEDURE [dbo].[GetAll]
    
    AS
    BEGIN
     SET NOCOUNT ON;
     SELECT * FROM Customers
     SELECT * FROM Orders
     SELECT * FROM ProductsEND

    The Stored PROCEDURE returns three tables, if you want to use the SqlCommand  object , you don't use ExecuteReader(). the best way  is that you change your Sql statement and make it return a single table result.

    >>1. i would suggest you try use DataSet and fill the DataSet. Then the DataSet would get three tables , default Table0,Table1,Table2 etc.

    >>2. If you use Linq to Sql, i would suggest you try the codes:

    // 6. Execute the command and materialize the car entitiesusing(IDataReader dataReader = oaCommand.ExecuteReader()){List<Customer> cars = dbContext.Translate<Customer>( dataReader asDbDataReader).ToList();// 7. Advance to the next result sets
                           dataReader.NextResult();List<Order>Orders= dbContext.Translate<Order>( dataReader asDbDataReader).ToList(); ...}

    check here:

    How to: Execute Stored Procedures Returning Multiple Result Sets

    >>3. If you use Entity Framework, i would suggest you try the codes:

    // 6. Execute the command and materialize the car entitiesusing(IDataReader dataReader = oaCommand.ExecuteReader()){// ((IObjectContextAdapter)db)//            .ObjectContext//            .Translate<Blog>(reader, "Blogs", MergeOption.AppendOnly);List<Customer> cars =((IObjectContextAdapter)dbContext).ObjectContext.Translate<Customer>(dataReader asDbDataReader).ToList();// List<Customer> cars = dbContext.Translate<Customer>(dataReader as DbDataReader).ToList();// 7. Advance to the next result sets
                            dataReader.NextResult();List<Order>Orders=((IObjectContextAdapter)dbContext).ObjectContext.Translate<Order>(dataReader asDbDataReader).ToList();
    
                            dataReader.NextResult();List<Product>Products=((IObjectContextAdapter)dbContext).ObjectContext.Translate<Product>(dataReader asDbDataReader).ToList();// List<Category> categories = dbContext.Translate<Category>(dataReader as DbDataReader).ToList();}

    check here:

    How to: Stored Procedures with Multiple Result Sets

    http://msdn.microsoft.com/en-us/data/jj691402.aspx

    :

    http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

  • 相关阅读:
    C# 根据实体将DataTable拆分成表头表体
    html KeyDown 当键盘按下时 获取所按键的Code
    Linq ExecuteQuery,ExecuteCommand
    Linq To Object
    Var To DataTable
    Linq to SQL 基础篇
    反射生成SQL语句
    Ajax跨域请求ashx文件与Webservice文件
    Ajax调用WebService
    DataTableToJson
  • 原文地址:https://www.cnblogs.com/happy-Chen/p/3635987.html
Copyright © 2011-2022 走看看