I have some code that fetches relatively large amounts of data from my SQL db (approx 200k records, 7 columns) which I then have to act upon and update / insert into other tables.
My initial iteration is done by opening a SqlDataReader and looping over it - it would appear though that this seems to hold an open transaction on the db and can cause some locking issues when the process takens several hours to run. The data is fetched via a sql stored proc and I am pretty sure that's pretty well optimised. The processing for each record is fairly intensive.
My pseudo code :
string sql = "EXEC StoredProc"
sqlConn.Open();
SqlCommand sqlComm = new SqlCommand(sql, sqlConn);
SqlDataReader reader = sqlComm.ExecuteReader();
//loop through products
while (reader.Read())
{
// do stuff
}
Can I put the SQLDataReader into an array or List<> to grab it "offline" so the db is freed from waiting for all the code in between to run several hundred thousand times - will that improve matters or make it worse because there will be so much data held in memory?