2013-11-20 102 views

回答

0

嘗試下面的代碼:

using (var db = new customerContext()) 
{ 

    // Create a SQL command to execute the stored procedure 
    var cmd = db.Database.Connection.CreateCommand(); 
    cmd.CommandText = "[dbo].[GetAllCustomerAndOrders]"; 

    try 
    { 

     db.Database.Connection.Open(); 
     // Run the stored procedure 
     var reader = cmd.ExecuteReader(); 

     // Read customer from the first result set 
     var customers = ((IObjectContextAdapter)db) 
      .ObjectContext 
      .Translate<Customer>(reader, "Customers", MergeOption.AppendOnly); 


     foreach (var item in customers) 
     { 
      Console.WriteLine(item.Name); 
     }   

     // Move to second result set and read orders 
     reader.NextResult(); 
     var orders = ((IObjectContextAdapter)db) 
      .ObjectContext 
      .Translate<Order>(reader, "Orders", MergeOption.AppendOnly); 


     foreach (var item in orders) 
     { 
      Console.WriteLine(item.OrderNumber); 
     } 
    } 
    finally 
    { 
     db.Database.Connection.Close(); 
    } 
} 
+0

我們可以使用的數據集,而不是DataReader的? –

+0

我沒有嘗試,但我認爲你可以使用它!但是你必須使用Adapter並填充DataSet並遍歷數據表。 – SoftSan

相關問題