I am working on an existing application. This application reads data from a huge file and then, after performing some calculations, saves the data in another table.
But the loop doing this (see below) takes a lot of time. Since the file sometimes contains 1000 entries, the whole process takes several days.
Is it possible to replace this foreach with something else? I tried using Parallel.ForEach and it helped. I am new to this, so appreciate your help.
foreach (record someredord Somereport.r) { try { using (var command = new SqlCommand("[procname]", sqlConn)) { command.CommandTimeout = 0; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(β¦); IAsyncResult result = command.BeginExecuteReader(); while (!result.IsCompleted) { System.Threading.Thread.Sleep(10); } command.EndExecuteReader(result); } } catch (Exception e) { β¦ } }
After looking at the answers, I uninstalled Async and used the edited code as shown below. But this did not improve performance.
using (command = new SqlCommand("[sp]", sqlConn)) { command.CommandTimeout = 0; command.CommandType = CommandType.StoredProcedure; foreach (record someRecord in someReport.) { command.Parameters.Clear(); command.Parameters.Add(....) command.Prepare(); using (dr = command.ExecuteReader()) { while (dr.Read()) { if () { } else if () { } } } } }
user1110790
source share