2017-09-04 24 views
0

私は約14の異なる結果セットを持つストアドプロシージャを持っています。どのようにしてそれらをすべて取り出すのですか?最初の結果セットのみを取得します。linqを使用して複数の結果セットを取得する方法

[HttpGet] 
[Route("tire-tabel")] 
public List<DeviationCalculation_Result> TireTabel(decimal presentWidth, decimal presentAspectRatio, string presentRimSize, int maxDeviation) 
{ 
    using (var context = new OminiTireEntities()) 
    { 
     var result = context.Database.SqlQuery<DeviationCalculation_Result>(
"exec [Tabel].[DeviationCalculation] @PresentWidth = '" + presentWidth + "', " + 
"@PresentAspectRatio= '" + presentAspectRatio + "', " + 
"@PresentInches= '" + presentRimSize + "', " + 
"@MaxDeviation= '" + maxDeviation + "'").ToList<DeviationCalculation_Result>(); 
     return result; 
    } 
} 
+0

'[Tabel]。[DeviationCalculation]'のコードは何ですか?あなたは 'where'条件に入ると思ういくつかのパラメータ値を渡していて、フィルタリングされた結果を返しますか? –

+0

円周を計算するために数学的関数に入るのではなく、あらかじめ定義された15の円周の偏差 –

答えて

1

サンプルコード:

using (var db = new BloggingContext()) 
{ 
    // If using Code First we need to make sure the model is built before we open the connection 
    // This isn't required for models created with the EF Designer 
    db.Database.Initialize(force: false); 

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

    try 
    { 

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

     // Read Blogs from the first result set 
     var blogs = ((IObjectContextAdapter)db) 
      .ObjectContext 
      .Translate<Blog>(reader, "Blogs", MergeOption.AppendOnly); 


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

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


     foreach (var item in posts) 
     { 
      Console.WriteLine(item.Title); 
     } 
    } 
    finally 
    { 
     db.Database.Connection.Close(); 
    } 
} 

翻訳方法は、我々は手順、のEntitySet名、MergeOptionを実行したとき、我々は受信されたリーダーを受け付けます。 EntitySetの名前は、派生コンテキストのDbSetプロパティと同じになります。 MergeOption enumは、同じエンティティがすでにメモリに存在する場合の結果の処理方法を制御します。

参考:https://msdn.microsoft.com/en-us/library/jj691402(v=vs.113).aspx

私もそれがDapperでSQLインジェクション

0

につながることができますよう、質問で述べたように代わりにクエリを実行するのでパラメータを使用することをお勧めします、それは超簡単です:

public DeviationCalculationResult Get(decimal presentWidth, decimal presentAspectRatio, string presentRimSize, int maxDeviation) 
{ 
    using (var context = new OminiTireEntities()) 
    { 
     var reader = context.Database.Connection.QueryMultiple("[Tabel].[DeviationCalculation]", 
      new 
      { 
       PresentWidth = presentWidth, 
       PresentAspectRatio = presentAspectRatio, 
       PresentInches = presentRimSize, 
       MaxDeviation = maxDeviation 
      }, commandType: CommandType.StoredProcedure); 

     var first = reader.Read<First>().ToList().First(); 
     var second = reader.Read<Second>().ToList().First(); 
     var third = reader.Read<Third>().ToList().First(); 
     //...and so on... 

     return new DeviationCalculationResult 
     { 
      First = first, 
      Second = second, 
      Third = third, 
      //... 
     }; 
    } 
} 
関連する問題