开发者

What is the fastest code to call multiple stored procedures?

Solution 1 :

foreach (var item in itemList)
{
   myContext.ExecuteStoreCommand("EXEC MyProc {0};", item); // Insertion
}

or

Solution 2 :

StringBuilder sb = new StringBuilder();
foreach (var item in itemList)
{
   sb.AppendLine(String.Format("EXEC M开发者_StackOverflow中文版yProc {0};", item)); // Insertion
}
myContext.ExecuteStoreCommand(sb.ToString());


Second is faster (one call to the database instead of multiple), first is safer since it protects against SQL injection.


both are second one is subject to sql injection, that is for sure

by reading this and this, I agree with kekekela


Solution 2 is faster as its only a single call to myContext.ExecuteStoreCommand so less overhead from method calls through the context object


I would guess Solution 2 because there is less I/O between your application and the database. If speed is all you're concerned with, you could check for yourself by debugging with the System.Diagnostics.Stopwatch utility.


This sounds like an opportunity for a small optimization which is easily tested.

Likely they are very close to the same speed.


The one you haven't mentioned.

Prepare an SqlCommand with the stored procedure's name as the CommandText, the CommandType set to CommandType.StoredProcedure, and the params appropriately added.

Besides the performance gain from not using an ad-hoc query (that'd have to be reparsed each time), you'll also nip most of your current SQL injection issues in the bud.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜