I have a table with over 100000 records. I want to read the data, process them, and then delete the row if the process is successful, and update the row with an error code if it fails.
My approach is to get 1000 records at a time, load it to a datatable, pass this to a function that runs the process in 5 parallel threads. Once the first 1000 records are processed, open a new DataReader and work on the next 1000...and so on...
Does my approach look right? The issue I am having is that the code runs fine, but the 1000 records are not completely processed. About 300 records are processed and the rest remain. What am I doing wrong here?
using (SqlDataReader rdr = cmd.ExecuteReader) {
if (rdr.HasRows) {
Datatable dt = new Datatable();
dt.Load(rdr);
//process data
results = Process(dt);
}
}
public List<string> Process(Datatable dt) {
var options = new ParallelOptions() {
MaxDegreeOfParallelism = 5
}
List<string> results = new List<string>();
Parallel.ForEach(dt.Rows.Cast<DataRow>(), options, (trans) => {
//process and then delete/update row
....
results.Add(transResult);
});
return results
}